Skip to content

Instantly share code, notes, and snippets.

@EGreg
EGreg / gist:1678415
Created January 25, 2012 20:17 — forked from fennb/gist:1124580
Asynchronous loop issues illustrative example
// Illustrative case
// YEAH -- ILLUSTRATIVE OF THE FACT THAT YOU CAN ISSUE PARALLEL QUERIES TO THE DATABASE INSTEAD OF SERIAL ONES LIKE A DUMB THREADED IMPLEMENTATION THAT IS INFERIOR FOR DOING WEB SERVERS
function (recentBlogPostIds, callback) { // obviously you need this, but use callback instead of return
var results = {};
var count = 0;
for (var i = 0; i < recentBlogPostIds.length; i++) {
var blogPostId = recentBlogPostIds[i];
@EGreg
EGreg / DRY
Created January 25, 2012 20:13 — forked from fennb/gist:1124579
Asynchronous cache failover example
// asynchronous version
asynchronousCache.get("id:3244", function(err, myThing) {
if (myThing == null) {
asynchronousDB.query("SELECT * from something WHERE id = 3244", function(err, myThing) {
// We now have a thing from DB, do something with result
doSomething(myThing);
});
} else {
// We have a thing from cache, do something with result