Skip to content

Instantly share code, notes, and snippets.

@fujimura
Created February 5, 2014 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujimura/8816509 to your computer and use it in GitHub Desktop.
Save fujimura/8816509 to your computer and use it in GitHub Desktop.
Q = require 'q'
total = 0
delay = (ms) =>
deferred = Q.defer()
setTimeout( =>
total = total + ms
console.log "Delayed #{ms} msec. Spent #{total} msec."
deferred.resolve()
, ms)
deferred.promise
h1 = delay(100)
h1.then(-> delay 200)
.then(-> delay 300)
.then(-> delay 300)
.done(-> console.log "done")
@fujimura
Copy link
Author

fujimura commented Feb 5, 2014

$ coffee q.coffee                                                                                                                                ~/n master
Delayed 100 msec. Spent 100 msec.
Delayed 200 msec. Spent 300 msec.
Delayed 300 msec. Spent 600 msec.
Delayed 300 msec. Spent 900 msec.
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment