1 | export interface Options {
|
2 | /**
|
3 | Number of concurrently pending promises returned by `mapper`.
|
4 |
|
5 | Must be an integer from 1 and up or `Infinity`.
|
6 |
|
7 | @default Infinity
|
8 | */
|
9 | readonly concurrency?: number;
|
10 |
|
11 | /**
|
12 | When `true`, the first mapper rejection will be rejected back to the consumer.
|
13 |
|
14 | When `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.
|
15 |
|
16 | Caveat: When `true`, any already-started async mappers will continue to run until they resolve or reject. In the case of infinite concurrency with sync iterables, *all* mappers are invoked on startup and will continue after the first rejection. [Issue #51](https://github.com/sindresorhus/p-map/issues/51) can be implemented for abort control.
|
17 |
|
18 | @default true
|
19 | */
|
20 | readonly stopOnError?: boolean;
|
21 | }
|
22 |
|
23 | /**
|
24 | Function which is called for every item in `input`. Expected to return a `Promise` or value.
|
25 |
|
26 | @param element - Iterated element.
|
27 | @param index - Index of the element in the source array.
|
28 | */
|
29 | export type Mapper<Element = any, NewElement = unknown> = (
|
30 | element: Element,
|
31 | index: number
|
32 | ) => NewElement | Promise<NewElement>;
|
33 |
|
34 | /**
|
35 | @param input - Synchronous or asynchronous iterable that is iterated over concurrently, calling the `mapper` function for each element. Each iterated item is `await`'d before the `mapper` is invoked so the iterable may return a `Promise` that resolves to an item. Asynchronous iterables (different from synchronous iterables that return `Promise` that resolves to an item) can be used when the next item may not be ready without waiting for an asynchronous process to complete and/or the end of the iterable may be reached after the asynchronous process completes. For example, reading from a remote queue when the queue has reached empty, or reading lines from a stream.
|
36 | @param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value.
|
37 | @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 fulfilled values returned from `mapper` in `input` order.
|
38 |
|
39 | @example
|
40 | ```
|
41 | import pMap from 'p-map';
|
42 | import got from 'got';
|
43 |
|
44 | const sites = [
|
45 | getWebsiteFromUsername('sindresorhus'), //=> Promise
|
46 | 'https://avajs.dev',
|
47 | 'https://github.com'
|
48 | ];
|
49 |
|
50 | const mapper = async site => {
|
51 | const {requestUrl} = await got.head(site);
|
52 | return requestUrl;
|
53 | };
|
54 |
|
55 | const result = await pMap(sites, mapper, {concurrency: 2});
|
56 |
|
57 | console.log(result);
|
58 | //=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
|
59 | ```
|
60 | */
|
61 | export default function pMap<Element, NewElement>(
|
62 | input: AsyncIterable<Element | Promise<Element>> | Iterable<Element | Promise<Element>>,
|
63 | mapper: Mapper<Element, NewElement>,
|
64 | options?: Options
|
65 | ): Promise<Array<Exclude<NewElement, typeof pMapSkip>>>;
|
66 |
|
67 | /**
|
68 | Return this value from a `mapper` function to skip including the value in the returned array.
|
69 |
|
70 | @example
|
71 | ```
|
72 | import pMap, {pMapSkip} from 'p-map';
|
73 | import got from 'got';
|
74 |
|
75 | const sites = [
|
76 | getWebsiteFromUsername('sindresorhus'), //=> Promise
|
77 | 'https://avajs.dev',
|
78 | 'https://example.invalid',
|
79 | 'https://github.com'
|
80 | ];
|
81 |
|
82 | const mapper = async site => {
|
83 | try {
|
84 | const {requestUrl} = await got.head(site);
|
85 | return requestUrl;
|
86 | } catch {
|
87 | return pMapSkip;
|
88 | }
|
89 | };
|
90 |
|
91 | const result = await pMap(sites, mapper, {concurrency: 2});
|
92 |
|
93 | console.log(result);
|
94 | //=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']
|
95 | ```
|
96 | */
|
97 | export const pMapSkip: unique symbol;
|