Skip to content

Instantly share code, notes, and snippets.

@jxm262
Created March 16, 2015 19:13
Show Gist options
  • Save jxm262/98810174efe6ce75bcf3 to your computer and use it in GitHub Desktop.
Save jxm262/98810174efe6ce75bcf3 to your computer and use it in GitHub Desktop.
var Promise = require('bluebird');
var superagent = Promise.promisifyAll(require('superagent'));
function a(obj) {
return superagent
.get('http://google.com')
.endAsync();
};
function chainingClosure(start) {
var p = a(start)
.then(function (val) {
console.log('run first then.. ' + val.status);
return (start === 'without cancel() call') ? a('running next then') : p.cancel();
})
.then(function (val) {
console.log(val.status);
console.log('--------------');
})
.cancellable()
.catch(function () {
console.log('was cancelled');
});
return p;
};
Promise.resolve()
.then(function () {
return chainingClosure('without cancel() call');
})
.then(function () {
return chainingClosure('run with cancel');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment