Skip to content

Instantly share code, notes, and snippets.

@KevinTCoughlin
Forked from steve-taylor/doSynchronousLoop.js
Created October 9, 2013 14:03
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 KevinTCoughlin/6901825 to your computer and use it in GitHub Desktop.
Save KevinTCoughlin/6901825 to your computer and use it in GitHub Desktop.
/**
* Process an array of data synchronously.
*
* @param data An array of data.
* @param processData A function that processes an item of data.
* Signature: function(item, i, callback), where {@code item} is the i'th item,
* {@code i} is the loop index value and {@code calback} is the
* parameterless function to call on completion of processing an item.
*/
function doSynchronousLoop(data, processData, done) {
if (data.length > 0) {
var loop = function(data, i, processData, done) {
processData(data[i], i, function() {
if (++i < data.length) {
loop(data, i, processData, done);
} else {
done();
}
});
};
loop(data, 0, processData, done);
} else {
done();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment