如何在此处停止链接中的 promise :
.then(resp => {
if (xxxxx) {
return nextPromise()
} else {
// stop promise chain
return
}
})
.then(resp => {
// nextPromise callback function..
})
我以为返回会停止链条,但我错了。
最佳答案
如果您不想通过抛出错误来破坏链,解决方案是嵌套链:
.then(resp => {
if (xxxxx) {
return nextPromise().then(resp => {
// nextPromise callback function..
});
}
})
这仍然允许全局 .catch()
,您不必显式检查抛出的错误来结束链。一个缺点是,如果您有很多这样的条件,您最终会得到类似 "callback hell" 的结果。 .
关于javascript - then 回调函数中的破坏 promise 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43133420/