Created
September 30, 2012 14:03
-
-
Save fgalassi/3806816 to your computer and use it in GitHub Desktop.
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
// ==================================== | |
// 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