adkt
2/15/2018 - 10:23 PM

Multiple Promise Chains with Cancel

Change ("A","A") to ("A","B") at any step in the chain to have the promise chain cancel.

//npm install bluebird
var Promise = require("bluebird");
 
// Enable cancellation
Promise.config({cancellation: true});

var promiseChain = myPromise1("A", "A")
        .then((result) => {
            if (!result){
                return promiseChain.cancel();
            }
            return myPromise2("A", "A");
        })
        .then((result) => {
            if (!result){
                return promiseChain.cancel();
            }
            return myPromise3("A", "A");
        });

var myPromise1 = function (var1, var2) {
    var promise = new Promise((resolve, reject)=>{
        if(var1 == var2){
            console.log("PROMISE 1");
            resolve(true);
        }
        else {
            resolve(false);
        }
    });
    return promise;
}
 
var myPromise2 = function (var1, var2) {
    var promise = new Promise((resolve, reject)=>{
        if(var1 == var2){
            console.log("PROMISE 2");
            resolve(true);
        }
        else {
            resolve(false);
        }
    });
    return promise;
}
 
var myPromise3 = function (var1, var2) {
    var promise = new Promise((resolve, reject)=>{
        if(var1 == var2){
            console.log("PROMISE 3");
            resolve(true);
        }
        else {
            resolve(false);
        }
    });
    return promise;
}