Skip to content

Instantly share code, notes, and snippets.

@shgtkshruch
Created February 9, 2014 03:24
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 shgtkshruch/8893745 to your computer and use it in GitHub Desktop.
Save shgtkshruch/8893745 to your computer and use it in GitHub Desktop.
Async JavaScript
$ ->
# use settimeout
console.log 0
setTimeout ->
console.log 'hi, 1'
setTimeout ->
console.log 'hi, 2'
, 1000
console.log 1
, 1000
# use jQuery deferred
sleep = (time) ->
d = new $.Deferred
setTimeout ->
d.resolve()
, time
d.promise()
console.log 0
sleep 1000
.then ->
console.log 1
sleep 1000
.then ->
console.log 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment