Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/p-map-series
Map over promises serially
https://github.com/sindresorhus/p-map-series
Last synced: about 1 month ago
JSON representation
Map over promises serially
- Host: GitHub
- URL: https://github.com/sindresorhus/p-map-series
- Owner: sindresorhus
- License: mit
- Created: 2016-10-21T04:26:03.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2021-04-09T06:29:45.000Z (over 3 years ago)
- Last Synced: 2024-05-22T18:06:59.716Z (6 months ago)
- Language: JavaScript
- Size: 12.7 KB
- Stars: 46
- Watchers: 8
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- promise-fun - p-map-series
README
# p-map-series
> Map over promises serially
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.
## Install
```
$ npm install p-map-series
```## Usage
```js
import pMapSeries from 'p-map-series';const keywords = [
getTopKeyword() //=> Promise
'rainbow',
'pony'
];let scores = [];
const mapper = async keyword => {
const score = await fetchScore(keyword);
scores.push(score);
return {keyword, score};
});console.log(await pMapSeries(keywords, mapper));
/*
[
{
keyword: 'unicorn',
score: 99
},
{
keyword: 'rainbow',
score: 70
},
{
keyword: 'pony',
score: 79
}
]
*/
```## API
### pMapSeries(input, mapper)
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.
#### input
Type: `Iterable`
Mapped over serially in the `mapper` function.
#### mapper(element, index)
Type: `Function`
Expected to return a value. If it's a `Promise`, it's awaited before continuing with the next iteration.
## Related
- [p-each-series](https://github.com/sindresorhus/p-each-series) - Iterate over promises serially
- [p-reduce](https://github.com/sindresorhus/p-reduce) - Reduce a list of values using promises into a promise for a value
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
- [More…](https://github.com/sindresorhus/promise-fun)---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.