以下两个函数在调用时行为相同。
function func1() {
return asyncFunc()
.then(() => {
// do something
});
}
function func2() {
return asyncFunc()
.then(() => {
// do something
}).catch((err) => {
throw err;
});
}
我知道 .catch() 只是 Promise.prototype.then(undefined, onRejected)
的语法糖 MDN docs 。但是,当您从 promise 链中省略 .catch 时,我对幕后实际发生的情况感到困惑。
当 promise 链中没有 .catch() 时,幕后实际发生了什么? .catch((err) => { throw err;});
是否以某种方式“神奇地”附加了?
最佳答案
.catch((err) => { throw err;})
不会执行任何操作,它只会重新抛出错误。因此 .catch
返回的 Promise 将再次被拒绝,并出现错误 err
:
Promise.reject(new Error('test'))
.catch(err => {
console.error(err)
throw err
})
.catch(err => {
console.error(err)
})
func1
将返回一个 Promise,该 Promise 可能会被该链中发生的某些事件拒绝。因此,func1
的调用者可能想要处理该错误,或者如果调用者进一步传递接收到的 Promise,并且不想处理该错误,那么调用者也可以省略 catch。
但是“拥有”该链的人(最后收到该链且不将其传递给其他任何人的人)负责处理拒绝案例。
function func1() {
return asyncFunc()
.then(() => {
// do something
});
}
function callerA() {
return func1()
}
function callerB() {
// callerB does not return the promise retuirned by callerA,
// and does not pass it to any other function, so it has to handle
// the rjection case
callerA().catch(err => {
})
}
拒绝时,js 环境将检查是否在链中的任何位置捕获了此错误。如果情况并非如此,则可能会抛出 UnhandledPromiseRejectionWarning
:
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
或者如果是浏览器,错误会记录在控制台中。
js 环境如何处理未处理的拒绝取决于,nodejs 目前发出此警告:
DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
正如它所说,将在未来版本中终止该应用程序。
对于浏览器来说,它不太可能在未来终止该选项卡。它很可能始终只在控制台中记录该错误,但您仍然应该编写代码,就好像未处理的拒绝会终止上下文一样。
关于javascript - 当您从 JavaScript Promises 中省略 .catch 时,实际上会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51943022/