Skip to content

Instantly share code, notes, and snippets.

Last active December 22, 2015 23:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
slow_random = (autocb) ->
await setTimeout defer(), 100
slow_buggy_random = (bugginess=0.1, cb) ->
# cannot return multi values (e.g., err and value), thus no autocb
await setTimeout defer(), 100
if Math.random() < bugginess
cb "boom"
cb false, Math.random()
# parallel
errors = {}
slow_random defer n
slow_random defer m
slow_buggy_random 0.1, defer errors.first, x
slow_buggy_random 0.1, defer errors.second, y
for i in [1..10]
slow_buggy_random 0.1, defer errors[i], ignored
for i, err of errors
if err
console.log "error: #{err} on #{i}"
# waterfall
# trivial: () ==> n
slow_random defer n
# wrong order: (cb, delay) ==>
setTimeout defer(), 100
# in and out: (bugginess) ==> err, n
slow_buggy_random 0.2, defer err, n
return err if err
# inline waterfall
do (autocb=defer(err)) ->
await slow_random defer n
await slow_random defer m
return "error: shrinkage" if n > m
return err if err
# parallel each
for i in [1..10]
slow_random defer ignored
# serial each
for i in [1..10]
await slow_random defer ignored
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment