在链接承诺时,通过 onRejected 处理程序处理错误是否算作解决方案?
Does handling an error through an onRejected handler counts as a resolution when chaining promises?
如果第一个承诺 returns 在错误回调中处理错误,那么在承诺链中是否会出现链式承诺 运行?
Promise.all(promises)
.then(
() => {
returnError();
},
error => {
logError();
})
.then(
() => {
willthisRun();
}
);
不,不会,第二个.then
不会运行willThisRun()
。来自 documentation:
Once a Promise is fulfilled or rejected, the respective handler function (onFulfilled or onRejected) will be called asynchronously (scheduled in the current thread loop). The behavior of the handler function follows a specific set of rules. If a handler function:
- returns a value, the promise returned by then gets resolved with the returned value as its value;
- doesn't return anything, the promise returned by then gets resolved with an undefined value;
- throws an error, the promise returned by then gets rejected with the thrown error as its value;
- [...]
第一个 .then
返回的承诺将被(引用)“以抛出的错误作为其值被拒绝”,调用第二个 .then
的 onRejected
处理程序(未定义),这将引发未捕获的异常。
Promise.all(promises)
.then(
() => {
returnError();
},
error => {
// this handler gets called because there is an error
logError();
})
.then(
// this promise receives the rejection, calling the onrejected argument, which is undefined
() => {
willthisRun();
}
);
如果第一个承诺 returns 在错误回调中处理错误,那么在承诺链中是否会出现链式承诺 运行?
Promise.all(promises)
.then(
() => {
returnError();
},
error => {
logError();
})
.then(
() => {
willthisRun();
}
);
不,不会,第二个.then
不会运行willThisRun()
。来自 documentation:
Once a Promise is fulfilled or rejected, the respective handler function (onFulfilled or onRejected) will be called asynchronously (scheduled in the current thread loop). The behavior of the handler function follows a specific set of rules. If a handler function:
- returns a value, the promise returned by then gets resolved with the returned value as its value;
- doesn't return anything, the promise returned by then gets resolved with an undefined value;
- throws an error, the promise returned by then gets rejected with the thrown error as its value;
- [...]
第一个 .then
返回的承诺将被(引用)“以抛出的错误作为其值被拒绝”,调用第二个 .then
的 onRejected
处理程序(未定义),这将引发未捕获的异常。
Promise.all(promises)
.then(
() => {
returnError();
},
error => {
// this handler gets called because there is an error
logError();
})
.then(
// this promise receives the rejection, calling the onrejected argument, which is undefined
() => {
willthisRun();
}
);