-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
37 lines (28 loc) · 958 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
36
37
console.log("Polyfill of Promise.race");
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Hello js series");
}, 100);
});
const promise2 = new Promise((resolve, reject) => {
reject("Hello js series TID");
});
const promise3 = 10;
// Promise.race([promise1, promise2, promise3])
// .then((data) => console.log(data))
// .catch((err) => console.error(err));
// Promise Race
// Input is the array of promises.
// Return the first one to be resolved or reject.
//polyfill of promise.race
Promise.myRace = function (arrayOfPromises) {
return new Promise((resolve, reject) => {
for (let i = 0; i < arrayOfPromises.length; i++) {
Promise.resolve(arrayOfPromises[i]).then(resolve, reject);
}
// promise.then((value)=>{resolve(value)},(error)=>{reject(error)})
});
};
Promise.myRace([promise1, promise3, promise2])
.then((data) => console.log(data))
.catch((err) => console.error(err));