Created
October 23, 2015 17:30
-
-
Save vorandrew/02f49aa699f32a8c9738 to your computer and use it in GitHub Desktop.
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
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