-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromise.js
105 lines (82 loc) · 1.66 KB
/
Promise.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const promise1=new Promise((resolve,reject)=>{
setTimeout(() => {
resolve(1)
},2001);
})
const promise2=new Promise((resolve,reject)=>{
setTimeout(() => {
resolve(2)
},3000);
})
const promise3=new Promise((resolve,reject)=>{
setTimeout(() => {
reject(3)
},500);
})
// console.time("start: ");
// promise1.then((a)=>{
// console.log(a);
// })
// .catch((e)=>{
// console.log(e);
// })
// promise2.then((a)=>{
// console.log(a);
// })
// .catch((e)=>{
// console.log(e);
// })
// promise3.then((a)=>{
// console.log(a);
// })
// .catch((e)=>{
// console.log(e);
// })
// console.timeEnd("start: ");
const fun=async function p1(){
try{
// console.time("start: ")
// const a=await promise1
// console.log(a);
// const b=await promise2
// console.log(b);
// const c=await promise3
// console.log(c);
// console.timeEnd("start: ")
const l=await Promise.allSettled([promise1,promise2,promise3])
console.log('++++++++++',l);
}
catch(e){
console.log(e);
}
finally{
console.log("running");
}
}
fun()
// Promise chaining
// console.time("start: ");
// promise1.then((a)=>{
// console.log(a);
// return promise2
// })
// .then((b)=>{
// console.log(b);
// return promise3
// })
// .then((c)=>{
// console.log(c);
// console.timeEnd("start: ")
// })
// .catch((e)=>{
// console.log(e);
// })
Promise.allSettled([promise1,promise2,promise3]).then((a)=>{
console.log("running a:",a);
console.log("running b:",b);
console.log("running c:",c);
})
.catch((e)=>{
console.log(e);
})
//allsettled,all