Skip to content

Instantly share code, notes, and snippets.

@joubertnel
Created January 11, 2012 21:53
Show Gist options
  • Save joubertnel/1596981 to your computer and use it in GitHub Desktop.
Save joubertnel/1596981 to your computer and use it in GitHub Desktop.
verbose coordination of async operations on a sequence
function readSource(after) {
var remainingWork;
var fileContents;
console.log('Reading source files...');
remainingWork = settings.src.length;
fileContents = new Array(remainingWork);
settings.src.forEach(function(f, index) {
fs.readFile(f, function(err, data) {
fileContents[index] = {'file': f,
'data': data};
remainingWork = remainingWork - 1;
if ((remainingWork === 0) && after) after(fileContents);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment