1 | import AggregateError from 'aggregate-error';
|
2 |
|
3 | export default async function pMap(
|
4 | iterable,
|
5 | mapper,
|
6 | {
|
7 | concurrency = Number.POSITIVE_INFINITY,
|
8 | stopOnError = true
|
9 | } = {}
|
10 | ) {
|
11 | return new Promise((resolve, reject_) => {
|
12 | if (iterable[Symbol.iterator] === undefined && iterable[Symbol.asyncIterator] === undefined) {
|
13 | throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof iterable})`);
|
14 | }
|
15 |
|
16 | if (typeof mapper !== 'function') {
|
17 | throw new TypeError('Mapper function is required');
|
18 | }
|
19 |
|
20 | if (!((Number.isSafeInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency >= 1)) {
|
21 | throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`);
|
22 | }
|
23 |
|
24 | const result = [];
|
25 | const errors = [];
|
26 | const skippedIndexesMap = new Map();
|
27 | let isRejected = false;
|
28 | let isResolved = false;
|
29 | let isIterableDone = false;
|
30 | let resolvingCount = 0;
|
31 | let currentIndex = 0;
|
32 | const iterator = iterable[Symbol.iterator] === undefined ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
|
33 |
|
34 | const reject = reason => {
|
35 | isRejected = true;
|
36 | isResolved = true;
|
37 | reject_(reason);
|
38 | };
|
39 |
|
40 | const next = async () => {
|
41 | if (isResolved) {
|
42 | return;
|
43 | }
|
44 |
|
45 | const nextItem = await iterator.next();
|
46 |
|
47 | const index = currentIndex;
|
48 | currentIndex++;
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | if (nextItem.done) {
|
57 | isIterableDone = true;
|
58 |
|
59 | if (resolvingCount === 0 && !isResolved) {
|
60 | if (!stopOnError && errors.length > 0) {
|
61 | reject(new AggregateError(errors));
|
62 | return;
|
63 | }
|
64 |
|
65 | isResolved = true;
|
66 |
|
67 | if (!skippedIndexesMap.size) {
|
68 | resolve(result);
|
69 | return;
|
70 | }
|
71 |
|
72 | const pureResult = [];
|
73 |
|
74 |
|
75 | for (const [index, value] of result.entries()) {
|
76 | if (skippedIndexesMap.get(index) === pMapSkip) {
|
77 | continue;
|
78 | }
|
79 |
|
80 | pureResult.push(value);
|
81 | }
|
82 |
|
83 | resolve(pureResult);
|
84 | }
|
85 |
|
86 | return;
|
87 | }
|
88 |
|
89 | resolvingCount++;
|
90 |
|
91 |
|
92 | (async () => {
|
93 | try {
|
94 | const element = await nextItem.value;
|
95 |
|
96 | if (isResolved) {
|
97 | return;
|
98 | }
|
99 |
|
100 | const value = await mapper(element, index);
|
101 |
|
102 |
|
103 | if (value === pMapSkip) {
|
104 | skippedIndexesMap.set(index, value);
|
105 | }
|
106 |
|
107 | result[index] = value;
|
108 |
|
109 | resolvingCount--;
|
110 | await next();
|
111 | } catch (error) {
|
112 | if (stopOnError) {
|
113 | reject(error);
|
114 | } else {
|
115 | errors.push(error);
|
116 | resolvingCount--;
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | try {
|
123 | await next();
|
124 | } catch (error) {
|
125 | reject(error);
|
126 | }
|
127 | }
|
128 | }
|
129 | })();
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | (async () => {
|
139 | for (let index = 0; index < concurrency; index++) {
|
140 | try {
|
141 |
|
142 | await next();
|
143 | } catch (error) {
|
144 | reject(error);
|
145 | break;
|
146 | }
|
147 |
|
148 | if (isIterableDone || isRejected) {
|
149 | break;
|
150 | }
|
151 | }
|
152 | })();
|
153 | });
|
154 | }
|
155 |
|
156 | export const pMapSkip = Symbol('skip');
|