-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromiseRace.js
More file actions
48 lines (42 loc) · 920 Bytes
/
PromiseRace.js
File metadata and controls
48 lines (42 loc) · 920 Bytes
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
44
45
46
47
48
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise 1 is resolved");
}, 500);
});
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject("Error in p2");
// resolve("Promise 2 is resolved");
}, 300);
});
const p3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise 3 is resolved");
}, 1000);
});
const p4 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise 4 is resolved");
}, 700);
});
const p5 = [p1, p2, p3, p4];
function PromiseRace(p5) {
return new Promise((resolve, reject) => {
p5.forEach((promise) => {
promise
.then((val) => {
resolve(promise);
})
.catch((err) => {
reject(promise);
});
});
});
}
PromiseRace(p5)
.then((val) => {
console.log(val);
})
.catch((error) => {
console.log(error);
});