蓝鸟承诺并抓住分支 [英] Bluebird promises and catch branching

查看:97
本文介绍了蓝鸟承诺并抓住分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Bluebird是否有办法承诺 .catch 抛出错误然后处理一些特定的操作而不进行分支(嵌套的承诺)。

I'm wondering if there a way in Bluebird promises to .catch a thrown error and then process some specific actions without branching (nested 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
})

当我执行此代码时,我会遇到以下几种行为:

When I execute this code, I get several behaviours:


  • 如果没有错误抛出,我会成功,太棒了!并且它再次开始让我们在经过一段长时间的延迟后再试一次

  • 如果CustomError抛出,我会让我们再试一次

  • 如果CustomError2抛出,我得到让我们在经过一段长时间的延迟后再试一次

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

I can't figure out what's happening with this flow. Should be great to write something like this instead of nesting the errors' specific code in new promise chains.

推荐答案


.catch 抛出错误,然后处理一些不分支的特定操作

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

没有。因为 分支。嵌套在这里是完全自然的。您甚至可以使用同步 - try-catch 这个比喻来考虑这一点。它将是相同的。

No. Because that is branching. Nesting is totally natural here. You could even think of this using the synchronous-try-catch metaphor and it would be the same.


我无法弄清楚这个流程发生了什么。

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


  • 如果没有抛出错误,我会成功,太棒了!它再次以让我们在经过一段长时间的延迟后再试

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

Hm, that's odd, because the "Let's try again" (without delay) is chained before that. You should eventually get both logs. Your chain is processed sequentially:

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





  • 如果抛出CustomError,我会让我们再试一次

是的,因为 saveSomethingAsync(); ,前一个承诺的结果已经完成,所以下一个 .then()链中的回调执行。

Yes, because saveSomethingAsync();, the result of the previous promise, is fulfilled, so the next .then() callback in the chain executes.



  • 如果抛出CustomError2,我会让我们试试经过一段长时间的延迟

是的,因为错误一直冒到 .catch(CustomError2,...)最终处理它的位置。在途中,没有执行回调。在处理错误之后,履行了承诺,下一个 .then()调用它的回调。

Yes, because the error bubbled all the way down to .catch(CustomError2, …) where it was eventually handled. On its way, no callbacks were executed. After the error was handled, the promise is fulfilled, and the next .then() in the chain calls its callback.

我认为你真正想要的是

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
})

这篇关于蓝鸟承诺并抓住分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆