Skip to content

Instantly share code, notes, and snippets.

@jxm262
Last active June 6, 2016 17:18
Show Gist options
  • Save jxm262/b150fa87840f56fd8284 to your computer and use it in GitHub Desktop.
Save jxm262/b150fa87840f56fd8284 to your computer and use it in GitHub Desktop.
SuperAgent + Bluebird.js
var Promise = require('bluebird'), 

superagent = Promise.promisifyAll(require('superagent'));
superagent.Request.prototype.cancellable = function () {

return this.endAsync().cancellable();
};



superagent.Request.prototype.then = function (done) {

return this.endAsync().then(done);

};
//option with the standard call to .endAsync
superagent
.get('http://google.com')
.endAsync()
.cancellable()
.then(function (done) {
console.log(done.status);
})
.catch(function (err) {
console.log(err);
});
//option if you want to return a promise directly from `then` or `cancellable()`
superagent
.get('http://google.com')
.cancellable()
.then(function (done) {
console.log(done.status);
})
.catch(function (err) {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment