gpt4 book ai didi

node.js - Bluebird - 如何尽早打破 promise 链

转载 作者:搜寻专家 更新时间:2023-10-31 22:19:38 25 4
gpt4 key购买 nike

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
})

如何修改上述代码以提前中断(跳过第 2 个 then())?

最佳答案

Bluebird 允许 cancel a promise :

var Promise = require('bluebird');
Promise.config({
// Enable cancellation
cancellation: true,
});

// store the promise
var p = PromiseA().then(function(dataA){
if (dataA.foo == "skip me")
p.cancel(); // cancel it when needed
else
return PromiseB();
}).then(function(dataB){
console.log(dataB);
}).catch(function (e) {
//Optimal solution will not cause this method to be invoked
});

关于node.js - Bluebird - 如何尽早打破 promise 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com