forked from weolwo/promise-learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_promise的几个关键问题5.js
44 lines (43 loc) · 1.14 KB
/
07_promise的几个关键问题5.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
/*
6.promise异常传透?
(1)当使用promise的then链式调用时, 可以在最后指定失败的回调,
(2)前面任何操作出了异常, 都会传到最后失败的回调中处理
7.中断promise链?
(1)当使用promise的then链式调用时, 在中间中断, 不再调用后面的回调函数
(2)办法: 在回调函数中返回一个pending状态的promise对象
*/
new Promise((resolve, reject) => {
// resolve(1)
reject(1)
}).then(
value => {
console.log('onResolved1()', value)
return 2
},
// reason => {throw reason}
).then(
value => {
console.log('onResolved2()', value)
return 3
},
reason => {
throw reason
}
).then(
value => {
console.log('onResolved3()', value)
},
reason => Promise.reject(reason)
).catch(reason => {
console.log('onReejected1()', reason)
// throw reason
// return Promise.reject(reason)
return new Promise(()=>{}) // 返回一个pending的promise 中断promise链
}).then(
value => {
console.log('onResolved3()', value)
},
reason => {
console.log('onReejected2()', reason)
}
)