2015-04-01 114 views
0

我想知道如果有蓝鸟的方式承诺.catch抛出的错误,然后处理一些没有分支(嵌套承诺)的具体行动。蓝鸟承诺并抓分支

说我有

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抛出,我得到“我们经过长期等待后重试”
再次启动

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

回答

1

.catch抛出的错误,然后处理一些具体的行动没有分支

号因为那分支。在这里嵌套是完全自然的。你甚至可以使用同步隐喻来想到这一点,它会是一样的。

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

  • 如果没有错误发生,我会得到“成功,太棒了!”并与“我们经过长期等待后重试”

嗯再次启动,这是奇怪的,因为“让我们再试一次”(无延迟)之前被链接。你最终应该得到两个日志。您链顺序处理:

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()回调执行。

  • 如果CustomError2被抛出,我得到“让我们再试一次经过一番长时间的延迟”

是的,因为错误一路冒泡到.catch(CustomError2, …)它最终被处理。在途中,没有回调被执行。在the error was handled之后,承诺完成,链中的下一个.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 
}) 
+0

好吧,我明白,承诺链顺序执行,我会去嵌套。 但是,我想继续抛出Errors而不立即处理它们,如果在带有嵌套promise的'.catch(CustomError,...)'中抛出一个错误,会发生什么? – EthanSbbn 2015-04-02 11:42:16

+0

与每个回调相同:'.catch()'返回的承诺被拒绝,并且链中的下一个事物将相应地执行。也许[此图](http://stackoverflow.com/a/24663315/1048572)也有帮助 – Bergi 2015-04-02 13:49:16