Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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