Created
July 16, 2014 13:09
-
-
Save colelawrence/c6d91a02246bcfaee2c6 to your computer and use it in GitHub Desktop.
Node style lifting function for promises
This file contains hidden or 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
| # 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