1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
|
3 |
|
|
const pMap = (iterable, mapper, options) => new Promise((resolve, reject) => {
|
4 |
|
|
options = Object.assign({
|
5 |
|
|
concurrency: Infinity
|
6 |
|
|
}, options);
|
7 |
|
|
|
8 |
|
|
if (typeof mapper !== 'function') {
|
9 |
|
|
throw new TypeError('Mapper function is required');
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
const {concurrency} = options;
|
13 |
|
|
|
14 |
|
|
if (!(typeof concurrency === 'number' && concurrency >= 1)) {
|
15 |
|
|
throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${concurrency}\` (${typeof concurrency})`);
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
const ret = [];
|
19 |
|
|
const iterator = iterable[Symbol.iterator]();
|
20 |
|
|
let isRejected = false;
|
21 |
|
|
let isIterableDone = false;
|
22 |
|
|
let resolvingCount = 0;
|
23 |
|
|
let currentIndex = 0;
|
24 |
|
|
|
25 |
|
|
const next = () => {
|
26 |
|
|
if (isRejected) {
|
27 |
|
|
return;
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
const nextItem = iterator.next();
|
31 |
|
|
const i = currentIndex;
|
32 |
|
|
currentIndex++;
|
33 |
|
|
|
34 |
|
|
if (nextItem.done) {
|
35 |
|
|
isIterableDone = true;
|
36 |
|
|
|
37 |
|
|
if (resolvingCount === 0) {
|
38 |
|
|
resolve(ret);
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
return;
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
resolvingCount++;
|
45 |
|
|
|
46 |
|
|
Promise.resolve(nextItem.value)
|
47 |
|
|
.then(element => mapper(element, i))
|
48 |
|
|
.then(
|
49 |
|
|
value => {
|
50 |
|
|
ret[i] = value;
|
51 |
|
|
resolvingCount--;
|
52 |
|
|
next();
|
53 |
|
|
},
|
54 |
|
|
error => {
|
55 |
|
|
isRejected = true;
|
56 |
|
|
reject(error);
|
57 |
|
|
}
|
58 |
|
|
);
|
59 |
|
|
};
|
60 |
|
|
|
61 |
|
|
for (let i = 0; i < concurrency; i++) {
|
62 |
|
|
next();
|
63 |
|
|
|
64 |
|
|
if (isIterableDone) {
|
65 |
|
|
break;
|
66 |
|
|
}
|
67 |
|
|
}
|
68 |
|
|
});
|
69 |
|
|
|
70 |
|
|
module.exports = pMap;
|
71 |
|
|
// TODO: Remove this for the next major release
|
72 |
|
|
module.exports.default = pMap;
|