Skip to content

Instantly share code, notes, and snippets.

@yshavit
Last active December 19, 2015 13:39
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 yshavit/5963744 to your computer and use it in GitHub Desktop.
Save yshavit/5963744 to your computer and use it in GitHub Desktop.
A solution to the "staircase problem" posed by node.js's reliance on callbacks. See http://blog.yuvalshavit.com/2013/07/tutorials-need-section-on-best-practices.html
# In some util class...
handler = (errText, f) ->
(err, rows) ->
if err?
console.log "#{errText}: #{err}"
else
f(rows)
# And then:
m = myqlPool()
barHandler = handler "Couldn't get a bar", (bars) ->
console.log("Found a bar!", bar) for bar in bars
fizzHandler = handler "Couldn't get fizzes", (fizzes) ->
foos = [fizz.foo for fizz in fizzes]
for foo in foos
do (foo) ->
m.query selectBar, [foo], barHandler
m.query getFizzes, fizzHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment