为了让这个问题对尽可能多的人有用,我将排除我的具体实现细节,超出了我在以下使用蓝鸟承诺库和Node + Express的事实。Bluebird Promise Chains:'Catch'与结果
所以,让我们说,我有以下的链(其中P
返回一个承诺,res
是快速HTTP响应对象):
P().then(function(){
// do nothing if all went well (for now)
// we only care if there is an error
}).catch(function(error){
res.status(500).send("An error occurred");
}).then(function(){
return P();
}).then(function(pVal1){
return [pVal1, P()];
}) // TODO: catch an error from P() here and log pVal1
.spread(function(pVal1, pVal2){
if(pVal1 === pVal2) {
console.log("Success!");
} else {
console.log("Failure");
}
});
当我把上面的TODO
评论是在那里我会喜欢发现我拨打P
时可能发生的错误。如果我遇到了错误,我想登录pVal1
,然后发送500错误,就像在第一次捕获中所做的那样。但是,我不确定这是否可能与我如何构建我的连锁店。
我相信我需要做一些“分支”,但我不认为我理解这个概念足够好,以阻止JavaScript的异步性质从我这里得到最好的结果!因此,任何帮助都被完全赞赏。
如果我这样做是完全错误的,请让我知道! – nmagerko
@torazaburo不,它不是。 –
@BenjaminGruenbaum为什么不呢? –