You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # p-map-series [![Build Status](https://travis-ci.org/sindresorhus/p-map-series.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map-series)
  2. > Map over promises serially
  3. Useful as a side-effect mapper. Use [`p-map`](https://github.com/sindresorhus/p-map) if you don't need side-effects, as it's concurrent.
  4. ## Install
  5. ```
  6. $ npm install --save p-map-series
  7. ```
  8. ## Usage
  9. ```js
  10. const pMapSeries = require('p-map-series');
  11. const keywords = [
  12. getTopKeyword() //=> Promise
  13. 'rainbow',
  14. 'pony'
  15. ];
  16. let scores = [];
  17. const mapper = keyword => fetchScore(keyword).then(score => {
  18. scores.push(score);
  19. return {keyword, score};
  20. });
  21. pMapSeries(keywords, mapper).then(result => {
  22. console.log(result);
  23. /*
  24. [{
  25. keyword: 'unicorn',
  26. score: 99
  27. }, {
  28. keyword: 'rainbow',
  29. score: 70
  30. }, {
  31. keyword: 'pony',
  32. score: 79}
  33. ]
  34. */
  35. });
  36. ```
  37. ## API
  38. ### pMapSeries(input, mapper)
  39. Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the `mapper` created promises fulfillment values.
  40. #### input
  41. Type: `Iterable<Promise|any>`
  42. Mapped over serially in the `mapper` function.
  43. #### mapper(element, index)
  44. Type: `Function`
  45. Expected to return a value. If it's a `Promise`, it's awaited before continuing with the next iteration.
  46. ## Related
  47. - [p-each-series](https://github.com/sindresorhus/p-each-series) - Iterate over promises serially
  48. - [p-reduce](https://github.com/sindresorhus/p-reduce) - Reduce a list of values using promises into a promise for a value
  49. - [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
  50. - [More…](https://github.com/sindresorhus/promise-fun)
  51. ## License
  52. MIT © [Sindre Sorhus](https://sindresorhus.com)