现在,我正在使用类似如下结构的代码:
doThis().then(() => {
doThat().then([...]).catch(e => {
console.log('internal catch');
});
}).catch(e => {
console.log('external catch');
});
如果内部函数doThat()
最终返回异常,内部捕获,外部捕获或两者都将发送消息吗?
答案 0 :(得分:0)
在您的情况下,只有内部catch才能处理doThat
函数调用引发的错误。外部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');
});