Created
March 14, 2014 17:12
-
-
Save warseph/9552253 to your computer and use it in GitHub Desktop.
Simple promises example
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
| Q = require 'q' | |
| receivePromise = (func) -> | |
| (args...) -> Q.spread args, func | |
| returnPromise = (func) -> | |
| (args...) -> | |
| deferred = Q.defer() | |
| args.push (val) -> deferred.resolve val | |
| func args... | |
| deferred.promise | |
| add = receivePromise returnPromise (a, b, cb) -> setTimeout (-> cb a + b), 3000 | |
| multiply = receivePromise returnPromise (a, b, cb) -> setTimeout (-> cb a * b), 3000 | |
| print = receivePromise (val) -> console.dir val | |
| result = add 4, 4 | |
| result2 = multiply result, 10 | |
| print result2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment