Skip to content

Instantly share code, notes, and snippets.

@fgalassi
Created September 30, 2012 14:03
Show Gist options
  • Save fgalassi/3806816 to your computer and use it in GitHub Desktop.
Save fgalassi/3806816 to your computer and use it in GitHub Desktop.
// ====================================
// Example: Nesting callbacks to produce serial requests
server.on('request', function(req, res) {
//get session information from memcached
memcached.getSession(req, function(session) {
//get information from db
db.get(session.user, function(userData) {
//some other web service call
ws.get(req, function(wsData) {
//render page
page = pageRender(req, session, userData, wsData);
//output the response
res.write(page);
});
});
});
});
// ====================================
// Example: Encapsulating within a callback
server.on('request', function(req, res) {
var render = function(wsData) {
page = pageRender(req, session, userData, wsData);
};
var getWsInfo = function(userData) {
ws.get(req, render);
};
var getDbInfo = function(session) {
db.get(session.user, getWsInfo);
};
var getMemCached = function(req, res) {
memcached.getSession(req, getDbInfo);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment