Skip to content

Instantly share code, notes, and snippets.

@vorandrew
Created October 23, 2015 17:30
Show Gist options
  • Save vorandrew/02f49aa699f32a8c9738 to your computer and use it in GitHub Desktop.
Save vorandrew/02f49aa699f32a8c9738 to your computer and use it in GitHub Desktop.
Promise = require 'bluebird'
co = require 'co'
# Callbacks
wait = (n, cb) ->
setTimeout ->
cb null, n
, 1000
wait 1, (err, res) ->
console.log "Callback - #{res}"
wait 2, (err, res) ->
console.log "Callback - #{res}"
wait 3, (err, res) ->
console.log "Callback - #{res}"
# Promises
Promise.resolve(Promise.delay(1000).then -> 1).then( (res) ->
console.log "Promise - #{res}"
Promise.resolve(Promise.delay(1000).then -> 2)
).then( (res) ->
console.log "Promise - #{res}"
Promise.resolve(Promise.delay(1000).then -> 3)
).then (res) ->
console.log "Promise - #{res}"
# Generators
co(->
res = yield Promise.resolve(Promise.delay(1000).then -> 1)
console.log "Yield - #{res}"
res = yield Promise.resolve(Promise.delay(1000).then -> 2)
console.log "Yield - #{res}"
res = yield Promise.resolve(Promise.delay(1000).then -> 3)
console.log "Yield - #{res}"
).catch (err) ->
console.log "Error", err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment