-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestpromise.js
124 lines (111 loc) · 2.48 KB
/
testpromise.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function promise1() {
return new Promise((resolve, reject) => {
setTimeout(function() {
resolve(1);
//reject(1);
}, 1000);
});
}
function promise2() {
return new Promise((resolve, reject) => {
setTimeout(function() {
resolve(2);
//reject(2);
}, 1000);
});
}
function promise3() {
return new Promise((resolve, reject) => {
setTimeout(function() {
resolve(3);
}, 1000);
});
}
function promise4() {
return new Promise((resolve, reject) => {
setTimeout(function() {
//resolve(4);
reject(4);
}, 1000);
});
}
// 1
// 2 -- resolve
// 3,4 async
function test() {
return new Promise((resolve, reject) => {
promise1()
.then((ret1) => {
console.log(ret1);
promise2()
.then((ret2) => {
console.log(ret2)
promise3()
.then((ret3) => {
console.log(ret3);
})
.catch((err) => {
console.error(`Error in promise3`);
reject(err);
});
promise4()
.then((ret4) => {
console.log(ret4);
})
.catch((err) => {
console.error(`Error in promise4`);
reject(err);
});
resolve('success!');
})
.catch((err) => {
console.error(`Error in promise2`);
reject(err);
});
})
.catch((err) => {
console.error(`Error in promise1`);
reject(err);
});
});
}
function test2() {
return new Promise((resolve, reject) => {
promise1()
.then((ret1) => {
console.log(ret1);
return promise2();
})
.then((ret2) => {
console.log(ret2);
promise3()
.then((ret3) => {
console.log(ret3);
})
.catch((err) => {
console.error(`Error in promise3`);
//reject(err); // these do nothing, since resolve already called!
});
promise4()
.then((ret4) => {
console.log(ret4);
})
.catch((err) => {
console.error(`Error in promise4`);
//reject(err);
});
resolve('success!');
})
.catch((err) => {
console.error(`catch all: ${err}`);
reject(err);
});
});
}
test2()
.then(res => {
console.log(res);
})
.catch(err => {
console.error(`ERROR: ${err}`);
});