-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathpromise-race.js
32 lines (30 loc) · 1.24 KB
/
promise-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
// Promise.race implementation
// Given an array of asynchronous functions, return a new promise.
// Each function in the array accepts no arguments and returns a promise.
// Race promise resolves or rejects with the first promise to settle.
/**
* @param {Array<Function>} functions
* @return {Promise<any>}
*/
const promiseRace = function (functions) {
if (!functions.length) {
return new Promise(() => {});
}
return new Promise((resolve, reject) => {
functions.forEach(fn => {
fn().then(result => resolve(result)).catch(reject);
});
});
};
promiseRace([() => new Promise(resolve => resolve(42))])
.then(console.log); // 42
promiseRace([
() => new Promise(resolve => setTimeout(() => resolve(1), 200)),
() => new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error')), 40))
]).then(console.log).catch(err => console.log(err.message)); // Error
promiseRace([
() => new Promise(resolve => setTimeout(() => resolve(4), 50)),
() => new Promise(resolve => setTimeout(() => resolve(10), 150)),
() => new Promise(resolve => setTimeout(() => resolve(16), 100)),
() => new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error')), 70))
]).then(console.log).catch(err => console.log(err.message)); // 4