Created
June 20, 2012 14:10
-
-
Save jbilcke/2960088 to your computer and use it in GitHub Desktop.
wait/async
This file contains 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
# 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