Projekt

Obecné

Profil

Stáhnout (2.21 KB) Statistiky
| Větev: | Revize:
1
# p-map [![Build Status](https://travis-ci.org/sindresorhus/p-map.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map)
2

    
3
> Map over promises concurrently
4

    
5
Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently.
6

    
7

    
8
## Install
9

    
10
```
11
$ npm install p-map
12
```
13

    
14

    
15
## Usage
16

    
17
```js
18
const pMap = require('p-map');
19
const got = require('got');
20

    
21
const sites = [
22
	getWebsiteFromUsername('sindresorhus'), //=> Promise
23
	'ava.li',
24
	'todomvc.com',
25
	'github.com'
26
];
27

    
28
(async () => {
29
	const mapper = async site => {
30
		const {requestUrl} = await got.head(site);
31
		return requestUrl;
32
	};
33

    
34
 	const result = await pMap(sites, mapper, {concurrency: 2});
35

    
36
	console.log(result);
37
	//=> ['http://sindresorhus.com/', 'http://ava.li/', 'http://todomvc.com/', 'http://github.com/']
38
})();
39
```
40

    
41
## API
42

    
43
### pMap(input, mapper, [options])
44

    
45
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.
46

    
47
#### input
48

    
49
Type: `Iterable<Promise|any>`
50

    
51
Iterated over concurrently in the `mapper` function.
52

    
53
#### mapper(element, index)
54

    
55
Type: `Function`
56

    
57
Expected to return a `Promise` or value.
58

    
59
#### options
60

    
61
Type: `Object`
62

    
63
##### concurrency
64

    
65
Type: `number`<br>
66
Default: `Infinity`<br>
67
Minimum: `1`
68

    
69
Number of concurrently pending promises returned by `mapper`.
70

    
71

    
72
## Related
73

    
74
- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
75
- [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently
76
- [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently
77
- [p-props](https://github.com/sindresorhus/p-props) - Like `Promise.all()` but for `Map` and `Object`
78
- [p-map-series](https://github.com/sindresorhus/p-map-series) - Map over promises serially
79
- [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
80
- [More…](https://github.com/sindresorhus/promise-fun)
81

    
82

    
83
## License
84

    
85
MIT © [Sindre Sorhus](https://sindresorhus.com)
(5-5/5)