Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created December 11, 2015 01:36
Show Gist options
  • Save vicapow/483abfae5691d5b44684 to your computer and use it in GitHub Desktop.
Save vicapow/483abfae5691d5b44684 to your computer and use it in GitHub Desktop.
'use strict';
function asyncTask(data, callback) {
process.setTimeout(function process() {
return callback(null, 'processed ' + data);
}, 100);
}
function doLotsOfStuff(callback) {
var tasks = ['task1', 'task2', 'task3'];
var results = [];
tasks.forEach(function each(task) {
asyncTask(task, function doneWithOneTask(error, result) {
results.push(result);
if (results.length === tasks.length) {
callback(error, results);
}
});
});
}
doLotsOfStuff(function done(error, results) {
if (error) {
throw error;
}
// do stuff with results
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment