gpt4 book ai didi

javascript - Bluebird promise 并捕获分支

转载 作者:行者123 更新时间:2023-12-03 10:26:14 39 4
gpt4 key购买 nike

我想知道 Bluebird 中是否有一种方法 promise .catch抛出的错误,然后处理一些特定的操作而不分支(嵌套 promise )。

说我有

doSomethingAsync()
.then(function (result) {
if (!result)
throw new CustomError('Blah Blah');

if (result == 0)
throw new CustomError2('Blah Blah Blah');

return result;
})
.then(function (result) {
console.log('Success, great!');
})

.catch(CustomError, function (error) {
// Oh CustomError!
return saveSomethingAsync();
})
.then(function (saved) {
// Should only be executed if an CustomError has been thrown
console.log("Let's try again");
return doSomethingAsync();
})

.catch(CustomError2, function (error) {
// Oh CustomError2!
})
.delay(15000) // Don't try again now !
.then(function () {
// Should only be executed if an CustomError2 has been thrown
console.log("Let's try again after some long delay");
return doSomethingAsync();
})

.catch(function (error) {
// Should catch all random errors of the chain
})

当我执行这段代码时,我得到了几种行为:

  • 如果没有错误抛出,我会得到“成功,太棒了!”然后它再次开始“让我们在长时间延迟后重试”
  • 如果抛出 CustomError,我会收到“让我们再试一次”
  • 如果 CustomError2 抛出异常,我会收到“让我们在长时间延迟后重试”

我无法弄清楚这个流程发生了什么。写这样的东西应该很棒,而不是将错误的特定代码嵌套在新的 promise 链中。

最佳答案

.catch a thrown error and then process some specific actions without branching

没有。因为那是分支。在这里嵌套是完全自然的。您甚至可以使用同步try-catch 比喻来思考这一点,结果是一样的。

I can't figure out what's happening with this flow.

  • If no error is thrown, I get "Success, great!" and it start again with "Let's try again after some long delay"

嗯,这很奇怪,因为“让我们再试一次”(没有延迟)是在此之前链接的。您最终应该获得两个日志。您的链将按顺序处理:

doSomethingAsync() // returns a result
then return result // first callback: continues with it
then console.log('Success, great!') // next callback: logs something
catch // is ignored because no rejection
then console.log("Let's try again"); // next callback: logs something
return doSomethingAsync(); // and returns a result
catch // that result, not being a rejection, is ignored here
delay // fulfillment is delayed
then console.log("Let's try again after some long delay"); // next callback again logs
return doSomethingAsync(); // and returns a result
catch // is ignored again
  • If a CustomError is thrown, I get "Let's try again"

是的,因为上一个 Promise 的结果 saveSomethingAsync(); 已实现,因此链中的下一个 .then() 回调将执行。

  • If a CustomError2 is thrown, I get "Let's try again after some long delay"

是的,因为错误一直冒泡到最终处理的 .catch(CustomError2, …) 。在途中,没有执行任何回调。之后the error was handled , promise 得到履行,链中的下一个 .then() 调用其回调。

我认为你真正想要的是

doSomethingAsync()
.then(function(result) {
if (!result)
// CustomError('Blah Blah');
// Oh CustomError!
return saveSomethingAsync();
.then(function(saved) {
console.log("Let's try again");
return doSomethingAsync();
});
else if (result == 0)
// CustomError2('Blah Blah Blah');
// Oh CustomError2!
return Promise.delay(15000) // Don't try again now !
.then(function() {
console.log("Let's try again after some long delay");
return doSomethingAsync();
})
else
console.log('Success, great!');
// return undefined implied
}).catch(function (error) {
// does catch all random errors of the chain
// thrown by any of the doSomethingAsync() or of the saveSomethingAsync
})

关于javascript - Bluebird promise 并捕获分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29387939/

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