Created
December 11, 2015 01:02
-
-
Save vicapow/6af4fce9f463dbb03316 to your computer and use it in GitHub Desktop.
This file contains 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
'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