UNPKG

3.65 kBMarkdownView Raw
1# p-map
2
3> Map over promises concurrently
4
5Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently.
6
7This is different from `Promise.all()` in that you can control the concurrency and also decide whether or not to stop iterating when there's an error.
8
9## Install
10
11```
12$ npm install p-map
13```
14
15## Usage
16
17```js
18import pMap from 'p-map';
19import got from 'got';
20
21const sites = [
22 getWebsiteFromUsername('sindresorhus'), //=> Promise
23 'https://avajs.dev',
24 'https://github.com'
25];
26
27const mapper = async site => {
28 const {requestUrl} = await got.head(site);
29 return requestUrl;
30};
31
32const result = await pMap(sites, mapper, {concurrency: 2});
33
34console.log(result);
35//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
36```
37
38## API
39
40### pMap(input, mapper, options?)
41
42Returns 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 fulfilled values returned from `mapper` in `input` order.
43
44#### input
45
46Type: `Iterable<Promise | unknown>`
47
48Iterated over concurrently in the `mapper` function.
49
50#### mapper(element, index)
51
52Type: `Function`
53
54Expected to return a `Promise` or value.
55
56#### options
57
58Type: `object`
59
60##### concurrency
61
62Type: `number` *(Integer)*\
63Default: `Infinity`\
64Minimum: `1`
65
66Number of concurrently pending promises returned by `mapper`.
67
68##### stopOnError
69
70Type: `boolean`\
71Default: `true`
72
73When set to `false`, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an [aggregated error](https://github.com/sindresorhus/aggregate-error) containing all the errors from the rejected promises.
74
75### pMapSkip
76
77Return this value from a `mapper` function to skip including the value in the returned array.
78
79```js
80import pMap, {pMapSkip} from 'p-map';
81import got from 'got';
82
83const sites = [
84 getWebsiteFromUsername('sindresorhus'), //=> Promise
85 'https://avajs.dev',
86 'https://example.invalid',
87 'https://github.com'
88];
89
90const mapper = async site => {
91 try {
92 const {requestUrl} = await got.head(site);
93 return requestUrl;
94 } catch {
95 return pMapSkip
96 }
97};
98
99const result = await pMap(sites, mapper, {concurrency: 2});
100
101console.log(result);
102//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
103```
104
105## p-map for enterprise
106
107Available as part of the Tidelift Subscription.
108
109The maintainers of p-map and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-p-map?utm_source=npm-p-map&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
110
111## Related
112
113- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
114- [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently
115- [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently
116- [p-props](https://github.com/sindresorhus/p-props) - Like `Promise.all()` but for `Map` and `Object`
117- [p-map-series](https://github.com/sindresorhus/p-map-series) - Map over promises serially
118- [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
119- [More…](https://github.com/sindresorhus/promise-fun)