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 })
解决方法@H_301_8@
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
});
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 });