Created
December 11, 2015 01:36
-
-
Save vicapow/483abfae5691d5b44684 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 = []; | |
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