在这种情况下我应该如何停止承诺链?只有当第一个 then 中的条件为真时,才执行第二个 then 的代码。
var p = new Promise((resolve, reject) => {
setTimeout(function() {
resolve(1)
}, 0);
});
p
.then((res) => {
if(true) {
return res + 2
} else {
// do something and break the chain here ???
}
})
.then((res) => {
// executed only when the condition is true
console.log(res)
})
原文由 vincentf 发布,翻译遵循 CC BY-SA 4.0 许可协议
You can
throw
anError
in theelse
block, thencatch
it at the end of the promise chain:演示 - https://jsbin.com/ludoxifobe/edit?js,console