Skip to content

Instantly share code, notes, and snippets.

@warseph
Created March 14, 2014 17:12
Show Gist options
  • Select an option

  • Save warseph/9552253 to your computer and use it in GitHub Desktop.

Select an option

Save warseph/9552253 to your computer and use it in GitHub Desktop.
Simple promises example
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