-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (27 loc) · 862 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
async function* raceIterator(promises) {
let leftToResolve = [];
for (const promise of promises) {
if (!promise || typeof promise !== 'object' || typeof promise.then !== 'function') {
// Yield non-promise-like value right away, since it doesn't need to be waited
yield promise;
} else {
const item = {status: 'pending'};
item.promise = promise.then((val, error) => {
item.status = 'resolved';
leftToResolve = leftToResolve.filter(item => item.status !== 'resolved')
if (error) {
throw error;
}
return val;
});
leftToResolve.push(item);
}
}
while (leftToResolve.length) {
yield await Promise.race(leftToResolve.map(item => item.promise));
}
}
Object.defineProperty(exports, '__esModule', {
value: true
});
exports['default'] = raceIterator;