Created
February 19, 2014 18:14
-
-
Save fiveisprime/9097959 to your computer and use it in GitHub Desktop.
A promise that also accepts a callback.
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
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