-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrace.js
43 lines (41 loc) · 1.43 KB
/
race.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
36
37
38
39
40
41
42
43
async function race(promises = []) {
if (promises.length === 0) {
setTimeout(() => {}, 10000);
}
return new Promise((resolve, reject) => {
promises.forEach((promise) => {
promise.then(resolve, reject);
});
});
}
// some: that takes an array of promises or values, and count number. It should return the first count resolved values. Empty arrays or a count of 0 return a promise resolving to undefined.
async function some(promises, count) {
if (promises.length === 0 || count === 0) {
return Promise.resolve([]);
}
return new Promise((resolve, reject) => {
var results = [];
let remaining = count;
promises.forEach((promise) => {
if (promise instanceof Promise) {
promise.then((result) => {
results.push(result);
remaining--;
if (remaining === 0) {
if (results[1] === undefined && results.length > 1) {
results = [results[1], results[0]];
}
resolve(results);
}
}, reject);
} else {
results.push(promise);
remaining--;
if (remaining === 0) {
resolve(results);
}
}
});
});
}
// Damn useless tests that make me commit again...