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抛出,我得到“我们经过长期等待后重试”
我无法弄清楚这个流程发生了什么。 应该很好写这样的东西,而不是将错误的特定代码嵌套在新的承诺链中。
好吧,我明白,承诺链顺序执行,我会去嵌套。 但是,我想继续抛出Errors而不立即处理它们,如果在带有嵌套promise的'.catch(CustomError,...)'中抛出一个错误,会发生什么? – EthanSbbn 2015-04-02 11:42:16
与每个回调相同:'.catch()'返回的承诺被拒绝,并且链中的下一个事物将相应地执行。也许[此图](http://stackoverflow.com/a/24663315/1048572)也有帮助 – Bergi 2015-04-02 13:49:16