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