[英]Best way to handle early returns in chains of promises?
在Bluebird中處理早期回報而又不會引發錯誤的最佳方法是什么。 例如,我在以下條件中有條件:
things.find(1)
.then(function(thing) {
if (thing.condition === true) {
return thing
} else {
// early return?
}
})
.then(function(thing) {
return doStuff(thing)
})
.then
鏈一旦形成,其自然行為便是隨着其每個階段的逐步建立而逐漸進行直至完成。
對於“早日歸來”(雖然不是一個好短語,但我們知道您的意思),您有以下三種選擇:
return new Promise()
.then
鏈響應(Esailija的答案) things.find(1).then(function(thing) {
if (!thing.condition) return;
return doStuff(thing)
.then(...);
.then(...);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.