-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromiseAllErrors.js
48 lines (41 loc) · 1.06 KB
/
promiseAllErrors.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
44
45
46
let p1 = new Promise(function(resolve, reject) {
//This will just reject half the time. Runs after 500ms.
setTimeout(function() {
if(Math.random() < 0.5) {
resolve("p1 success");
} else {
reject(new Error("p1 timeout promise rejected"));
}
}, 500);
});
let p2 = new Promise(function(resolve, reject) {
//This will resolve after 1000ms. Then throw an error half the time
setTimeout(function() {
resolve();
}, 1000);
})
.then(function() {
if(Math.random() < 0.5) {
//console.log("p2 success");
return "p2 success";
} else {
throw new Error("p2 error thrown after timeout");
}
});
/*
p1.then(function(result) {
console.log(result);
return p2;
});*/
/*
Promise.all([p1, p2])
.then(function(results) {console.log(results)})
.catch(function(err) {
console.log(err);
})
*/
Promise.allSettled([p1, p2])
.then(function(results) {console.log(results)})
.catch(function(err) {
console.log(err);
})