Created
March 16, 2015 19:13
-
-
Save jxm262/98810174efe6ce75bcf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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