Skip to content

Instantly share code, notes, and snippets.

@mikekunze
Created October 10, 2011 19:41
Show Gist options
  • Save mikekunze/1276317 to your computer and use it in GitHub Desktop.
Save mikekunze/1276317 to your computer and use it in GitHub Desktop.
Pseudo AsyncNess
// 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