Last active
December 19, 2015 13:39
-
-
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
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
# 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