【发布时间】:2016-09-03 19:31:14
【问题描述】:
PromiseA().then(function(dataA){
if (dataA.foo == "skip me")
return ?? //break promise early - don't perform next then()
else
return PromiseB()
}).then(function(dataB){
console.log(dataB)
}).catch(function (e) {
//Optimal solution will not cause this method to be invoked
})
如何修改上述代码以提前中断(跳过第二个 then())?
【问题讨论】:
-
return Promise.reject(new Error("please catch me"))
-
或者更好
throw new Error("please catch me");。这看起来像是重复的。 -
但在这种情况下会调用 catch(),这是我不想要的。
-
见stackoverflow.com/questions/28803287/how-to-break-promise-chain/… 使用
return { then: function() {} };