Last active
June 6, 2016 17:18
-
-
Save jxm262/b150fa87840f56fd8284 to your computer and use it in GitHub Desktop.
SuperAgent + Bluebird.js
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'), | |
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