Skip to content

Instantly share code, notes, and snippets.

@sheldonh
Last active December 20, 2015 04:59
Show Gist options
  • Select an option

  • Save sheldonh/6075402 to your computer and use it in GitHub Desktop.

Select an option

Save sheldonh/6075402 to your computer and use it in GitHub Desktop.
waterfall.coffee
waterfall = (i, fs, callback) ->
if fs.length is 0 then callback()
else if fs.length is 1 then fs[0] i, callback
else
p = 0
scroll = (e, o) ->
if e then callback(e)
else
p += 1
if p is fs.length - 1
fs[p](o, callback)
else
fs[p](o, scroll)
fs[p](i, scroll)
waterfall "H", [
(i, c) -> c(null, i + "e")
(i, c) -> c(null, i + "l")
(i, c) -> c(null, i + "l")
(i, c) -> c(null, i + "o")
], (e, o) -> console.log "Error:", e, "Output:", r
waterfall "O", [
(i, c) -> c(null, i + "h")
(i, c) -> c({name: 'Bugger', message: 'Somebody stop me'}, i + "noes")
(i, c) -> console.log "This does not happen"; c(null, i)
], (e, o) -> console.log "Error:", e, "Output:", r
###
Error: null Output: Hello
Error: { name: 'Bugger', message: 'Somebody stop me' } Output: undefined
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment