Created
October 10, 2011 19:41
-
-
Save mikekunze/1276317 to your computer and use it in GitHub Desktop.
Pseudo AsyncNess
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
// npm install async | |
// installs to ./node_modules | |
var db; // Pretend this is a valid async DB connection | |
var async = require('async'); | |
var bigArray = []; // Pretend this is full of data | |
var returnResults = []; // Pretend this is empty | |
// Secondary async function | |
var doStuff(item, cb) { | |
db.findOne({ item.id }, function(err, doc) { | |
// We have docs related to item.id | |
returnResults.push(doc); | |
cb(); | |
}); | |
}; | |
// Primary async item function, called by async.forEach below | |
var doFunction = function(item, callback) { | |
doStuff(item, callback); | |
}; | |
// Now that we've defined helper functions, lets actually do something | |
async.forEach(bigArray, doFunction, function() { | |
console.log(returnResults); // Will return results AFTER all iterations of | |
// forEach have been called back | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment