Skip to content

Instantly share code, notes, and snippets.

@troygoode
Last active July 22, 2016 09:07
Show Gist options
  • Save troygoode/cb82991c71eafa0c7323 to your computer and use it in GitHub Desktop.
Save troygoode/cb82991c71eafa0c7323 to your computer and use it in GitHub Desktop.
Promise Cancellation Example
// to run:
// $ npm install bluebird
// $ node cancel-promise.js
// example from: http://openmymind.net/Cancelling-Long-Promise-Chains/
(function () {
'use strict';
var Promise = require('bluebird');
function a(v) {
return Promise.resolve(v);
}
function b(v) {
return v * 2;
}
function c(v) {
return v * 2;
}
function chain(v) {
var p = a(v)
.then(function (value) {
console.log(value);
if (!value) {
return p.cancel();
}
return b(value);
})
.then(function (value) {
console.log(value);
return c(value);
})
.cancellable()
.catch(Promise.CancellationError, function () {
console.log('cancelled!');
return 0;
});
return p;
}
Promise.resolve()
.then(function () {
console.log('--------------------------------------------------------------------------------');
console.log('Chain [1]');
console.log('--------------------------------------------------------------------------------');
return chain(1)
.then(function (v) {
console.log('Chain [1]: %s', v); // will print 4
})
.catch(function (err) {
console.error('Chain [1] Error', err); // won't be called
});
})
.then(function () {
console.log('--------------------------------------------------------------------------------');
console.log('Chain [undefined]');
console.log('--------------------------------------------------------------------------------');
return chain(undefined) // same as saying `return chain()`, but I'm being explicit for illustration purposes
.then(function (v) {
console.log('Chain [undefined]: %s', v); // will print 0 because of cancellation handler
})
.catch(function (err) {
console.error('Chain [undefined] Error', err); // won't be called
});
});
}());
@btofel
Copy link

btofel commented May 19, 2016

Are you sure it works? When I try to run it I get Unhandled rejection TypeError: a(...).then(...).then(...).cancellable is not a function error. Nothing changes even if I add the following lines:
Promise.config({cancellable: true});

What's missing?

@mdsaleemj
Copy link

I tried

 'use strict';
var Promise =  require('bluebird');
Promise.config({
    cancellation :true
});

var myPromise = function(param){
    return new Promise(function(resolve,reject){
        if(param == null){
            return reject(new Error("rejected"));
        }
        setTimeout(function(){
           return resolve("resolved");
        },1000);

    })
}

var promise = myPromise("param");

//var promise = myPromise();

//console.log(promise);
promise
.then(function(message){
   console.log("message:",message); 
   promise.cancel();
   console.log(promise.isCancellable()); //returns false
   //throw new Promise.CancellationError("cancelled");
})
//.cancellable()
// .catch(function(err){
//    console.log("error",err);
// })
.then(function(message){
   //console.log("message",message);
   console.log("then 1 called");
})
.then(function(){
    console.log("then 2 called");
})
.catch(function(err){
    console.log("Error:",err);
})
.catch(Promise.CancellationError,function(err){
    console.log("cancelleation Error",err);
});

Response :

message: resolved
false
then 1 called
then 2 called

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment