Skip to content

Instantly share code, notes, and snippets.

@richthegeek
Last active December 26, 2015 17:29
Show Gist options
  • Save richthegeek/7187343 to your computer and use it in GitHub Desktop.
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.
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
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