Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created July 16, 2014 13:09
Show Gist options
  • Select an option

  • Save colelawrence/c6d91a02246bcfaee2c6 to your computer and use it in GitHub Desktop.

Select an option

Save colelawrence/c6d91a02246bcfaee2c6 to your computer and use it in GitHub Desktop.
Node style lifting function for promises
# Promises
{ Deferred } = require 'promise.coffee'
fs = require 'fs'
emptyPromise = ->
def = new Deferred
def.resolve(undefined)
return def.promise
nodeStyleLift = (nodefn) ->
->
def = new Deferred
args = [].slice.call arguments
args.push (err, res) ->
if err? then def.reject(err) else def.resolve(res)
->
nodefn.apply this, args
return def.promise
readFileAsync = nodeStyleLift fs.readFile
emptyPromise()
.then readFileAsync("package.json", "utf8")
.then (value) ->
console.log value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment