gpt4 book ai didi

javascript - catch() 和 then(successHandler, failureHandler) 的 "error handler"检测到什么错误?

转载 作者:行者123 更新时间:2023-11-30 19:37:25 27 4
gpt4 key购买 nike

我正在学习 Promises,以便在尝试使用 Firebase 之前能够更好地理解。我是新手,我一直在阅读有关 catch() 的以下内容:

  • Link one : 一篇带有一些练习的文章
  • Link two : SO 中的一个问题,关于为什么我们在 Promise 链之后总是需要一个 catch()
  • Link three : SO 中关于 catch()then() 的区别的问题

根据我所阅读的内容,我得出以下结论:

  • catch() 在每个 Promise 链中都是必需的,以防发生“意外异常”。我的thenfailureHandler 似乎可以检测到这些“意外异常”。但是,它无法区分“正常故障”和这些类型的故障。我假设这些“意外异常”之一是当您尝试访问 null 元素的某些属性时。
  • 似乎我也可以链接 then(successHandler, failureHandler) 然后继续 catch() block 以允许更好的控制,如 link two 中所述.当我想在某些事情失败时做其他事情(在这种情况下是“正常失败”,而不是“意外异常”)并将被拒绝的 Promise 传递给下一个 then 进行处理时,这很有用,因此可能产生与失败部分成功的结果截然不同的结果。如果在我的 successHandlerfailureHandler 中发生故障,我还可以通过在链的末尾使用 catch() 来捕获“意外异常”。

从我的结论中可以看出,我对可能发生的错误知之甚少。我提到 null 异常是“意外异常”的例子之一(这个假设是否正确?)。但是,failureHandler 检测到哪些其他错误以及 catch() 检测到哪些其他“意外异常”?

我在上面还提到,[then] 无法区分正常故障和这些类型的故障。那是对的吗?如果是,为什么它很重要?

编辑

在阅读更多内容后,似乎如果一个 Promise 在链的顶部被拒绝,则随后的 then 将被忽略,我立即转到 catch() block 。这意味着我上面的结论:当我想在某些事情失败时做其他事情并将被拒绝的 Promise 传递给下一个 then 处理时,这很有用/强 > 不正确。如果是这样的话,如果我的链的末尾已经有一个 catch(),那么我的每个 then< 就不再需要一个 failureHandler/ block 。但是,在链接三中提到了:

The argument is that usually you want to catch errors in every step of the processing, and that you shouldn't use it in chains. The expectation is that you only have one final handler which handles all errors - while, when you use the "antipattern", errors in some of the then-callbacks are not handled.

However, this pattern is actually very useful: When you want to handle errors that happened in exactly this step, and you want to do something entirely different when no error happened - i.e. when the error is unrecoverable. Be aware that this is branching your control flow. Of course, this is sometimes desired.

我的结论是被拒绝的Promise会被传递给下一个then来处理,因为我看了上面的内容。那么,并且您想在没有错误发生时(即错误无法恢复时)做一些完全不同的事情是什么意思

最佳答案

来自MDN我知道这两种声明拒绝处理程序的方法之间没有真正的区别。

为了成功,很明显我们将获得履行处理程序:

var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('success');
}, 300);
}).then(function(success) {
console.log(0, success)
}, function(failure) {
console.log(1, failure)
}).catch(function(failure) {
console.log(2, failure)
});

对于拒绝,我们将进入第一个拒绝处理程序。
拒绝 promise 时:

var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
reject('failure');
}, 300);
}).then(function(success) {
console.log(0, success)
}, function(failure) {
console.log(1, failure)
}).catch(function(failure) {
console.log(2, failure)
});

抛出错误时:

var promise = new Promise(function(resolve, reject) {
throw "throw";
}).then(function(success) {
console.log(0, success)
}, function(failure) {
console.log(1, failure)
}).catch(function(failure) {
console.log(2, failure)
});

请注意 catch block 是如何被忽略的,因为它被链接到第一个处理程序的 promise (履行或拒绝)。
如果我们将其更改为拒绝或重新抛出,我们将到达 catch 中的处理程序。

var promise = new Promise(function(resolve, reject) {
throw "re-throw";
}).then(function(success) {
console.log(0, success)
}, function(failure) {
console.log(1, failure)
throw failure;
}).catch(function(failure) {
console.log(2, failure)
});

这张图片来自上面的链接,描述得很好。 Image from MDN link

关于javascript - catch() 和 then(successHandler, failureHandler) 的 "error handler"检测到什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55789430/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com