Last active
December 27, 2015 12:29
-
-
Save glenjamin/7326445 to your computer and use it in GitHub Desktop.
How to optimise callbacks that need state
This file contains hidden or 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
var app = express(); | |
database.connect(function(err, db) { | |
app.db = db; | |
app.listen(1337); | |
}) | |
app.get('/stuff/:id', stuffHandler); | |
function stuffHandler(req, res) { | |
app.db.fetch(req.params.id, function datastoreCallback(err, data) { | |
res.send(data); | |
}) | |
} | |
// How do I perform an action on `res` without creating a new function on each request? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone following on at home, the answer is that ideally the callback API should provide something like:
Much like the javascript built-in setTimeout https://developer.mozilla.org/en/docs/Web/API/window.setTimeout#Syntax
This would allow me to do: