爲了讓這個問題對儘可能多的人有用,我將排除我的具體實現細節,超出了我在以下使用藍鳥承諾庫和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爲什麼不呢? –