Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created December 11, 2015 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicapow/6af4fce9f463dbb03316 to your computer and use it in GitHub Desktop.
Save vicapow/6af4fce9f463dbb03316 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 = [];
function checkForTasks() {
if (!tasks.length) {
return callback(results);
}
asyncTask(tasks.shift(), function doneWithOneTask(result) {
results.push(result);
checkForTasks();
});
}
checkForTasks();
}
doLotsOfStuff(function done() {
console.log('done with lots of stuff');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment