Skip to content

Instantly share code, notes, and snippets.

@fiveisprime
Created February 19, 2014 18:14
Show Gist options
  • Save fiveisprime/9097959 to your computer and use it in GitHub Desktop.
Save fiveisprime/9097959 to your computer and use it in GitHub Desktop.
A promise that also accepts a callback.
exports.get = function(fn) {
var deferred = Q.defer();
request('http://example.com', function(err, response) {
if (err) {
return deferred.reject(err);
}
if (response.statusCode !== 200) {
return deferred.reject(new Error(response.body || 'Response failed with code ' + response.statusCode));
}
deferred.resolve(response.body);
});
return deferred.promise.nodeify(fn);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment