[6a3a178] | 1 | declare namespace pMap {
|
---|
| 2 | interface Options {
|
---|
| 3 | /**
|
---|
| 4 | Number of concurrently pending promises returned by `mapper`.
|
---|
| 5 |
|
---|
| 6 | Must be an integer from 1 and up or `Infinity`.
|
---|
| 7 |
|
---|
| 8 | @default Infinity
|
---|
| 9 | */
|
---|
| 10 | readonly concurrency?: number;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | When 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.
|
---|
| 14 |
|
---|
| 15 | @default true
|
---|
| 16 | */
|
---|
| 17 | readonly stopOnError?: boolean;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | Function which is called for every item in `input`. Expected to return a `Promise` or value.
|
---|
| 22 |
|
---|
| 23 | @param element - Iterated element.
|
---|
| 24 | @param index - Index of the element in the source array.
|
---|
| 25 | */
|
---|
| 26 | type Mapper<Element = any, NewElement = unknown> = (
|
---|
| 27 | element: Element,
|
---|
| 28 | index: number
|
---|
| 29 | ) => NewElement | Promise<NewElement>;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | @param input - Iterated over concurrently in the `mapper` function.
|
---|
| 34 | @param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value.
|
---|
| 35 | @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.
|
---|
| 36 |
|
---|
| 37 | @example
|
---|
| 38 | ```
|
---|
| 39 | import pMap = require('p-map');
|
---|
| 40 | import got = require('got');
|
---|
| 41 |
|
---|
| 42 | const sites = [
|
---|
| 43 | getWebsiteFromUsername('https://sindresorhus'), //=> Promise
|
---|
| 44 | 'https://ava.li',
|
---|
| 45 | 'https://github.com'
|
---|
| 46 | ];
|
---|
| 47 |
|
---|
| 48 | (async () => {
|
---|
| 49 | const mapper = async site => {
|
---|
| 50 | const {requestUrl} = await got.head(site);
|
---|
| 51 | return requestUrl;
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | const result = await pMap(sites, mapper, {concurrency: 2});
|
---|
| 55 |
|
---|
| 56 | console.log(result);
|
---|
| 57 | //=> ['https://sindresorhus.com/', 'https://ava.li/', 'https://github.com/']
|
---|
| 58 | })();
|
---|
| 59 | ```
|
---|
| 60 | */
|
---|
| 61 | declare function pMap<Element, NewElement>(
|
---|
| 62 | input: Iterable<Element>,
|
---|
| 63 | mapper: pMap.Mapper<Element, NewElement>,
|
---|
| 64 | options?: pMap.Options
|
---|
| 65 | ): Promise<NewElement[]>;
|
---|
| 66 |
|
---|
| 67 | export = pMap;
|
---|