现在我正在使用类似于这样结构的代码:
doThis().then(() => {
doThat().then([...]).catch(e => {
console.log('internal catch');
});
}).catch(e => {
console.log('external catch');
});
如果内部函数doThat()最终返回异常,是内部捕获、外部捕获还是两者都发送消息?
现在我正在使用类似于这样结构的代码:
doThis().then(() => {
doThat().then([...]).catch(e => {
console.log('internal catch');
});
}).catch(e => {
console.log('external catch');
});
如果内部函数doThat()最终返回异常,是内部捕获、外部捕获还是两者都发送消息?
doThat在您的情况下,只有内部 catch 将处理函数调用引发的错误。外部doThis函数根本不知道发生了什么。如果您希望外部捕获来处理它,那么您将不得不重新组织您的代码。
doThis().then(() => {
// By returning the internal promise, the external one will
// now be able to handle both success and error.
return doThat().then([...]).catch(e => {
console.log('internal catch');
// Rethrowing will ensure that the error will propagate to the external handler.
// Otherwise, handling the error in the catch function will make
// the promise successful again.
throw e
});
}).catch(e => {
console.log('external catch');
});