Last active
December 26, 2015 17:29
-
-
Save richthegeek/7187343 to your computer and use it in GitHub Desktop.
A wrapper around the "request" module which allows use of the promises style for dealing with the response.
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
Q = require 'q' | |
request = require 'request' | |
init = request.Request::init | |
request.Request::init = (options) -> | |
defer = Q.defer() | |
@on 'complete', defer.resolve | |
@on 'error', defer.reject | |
# copy over promise functions, except timeout. | |
for key, val of defer.promise when key isnt 'timeout' | |
@[key] ?= defer.promise[key] | |
init.call @, options | |
module.exports = request |
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
request('http://www.example.com').then (response) -> | |
# do stuff with response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment