Skip to content

Instantly share code, notes, and snippets.

@jbilcke
Created June 20, 2012 14:10
Show Gist options
  • Save jbilcke/2960088 to your computer and use it in GitHub Desktop.
Save jbilcke/2960088 to your computer and use it in GitHub Desktop.
wait/async
# wait / don't wait
wait = (t=0) -> (f) -> setTimeout f, t
async = (f) -> setTimeout f, 0
# simple shortcut
log = console.log
class Main
compute: (a,b) -> a*b + a*b
example1: ->
# schedule functions to be executed
wait(500) -> log "hello"
wait(1000) -> log "world"
# example2 will be executed as soon as possible
example2: (txt) -> async ->
log "started"
log "finished"
# mix everything. this time using fat arrows
example3: (cb=->) => wait(4000) =>
log "long computation"
result = @compute()
async -> cb result
main = new Main()
main.example1()
main.example2()
main.example3()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment