这两个代码块相同吗?我希望打开 .catch()
并记录错误,但我仍然希望错误“未被捕获”,我可以返回它吗?或者是否需要将其包装在 Promise.reject()
中?
A区:
soSomething()
.then(() => {
return "meow"
})
.catch(() => {
console.log(err)
return err
})
B block :
soSomething()
.then(() => {
return "meow"
})
.catch(() => {
console.log(err)
return Promise.reject(err)
})
最佳答案
这两种模式并不相同。
第一个处理错误并返回已解析的 Promise,到达链接的 .then()
处的第一个函数参数。
第二个示例返回被拒绝的 Promise
,到达链接的 .then()
或 .catch()
处的第二个函数参数。
I'm looking to open up
.catch()
and log the error but I still want the error to be "uncaught", can I just return it?
第一个模式应该满足要求。
关于javascript - 打开后拒绝错误 `.catch`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45695100/