Skip to content

Instantly share code, notes, and snippets.

@gwendall
Created February 26, 2015 13:12
Show Gist options
  • Save gwendall/35787d6a74763f218f92 to your computer and use it in GitHub Desktop.
Save gwendall/35787d6a74763f218f92 to your computer and use it in GitHub Desktop.
Simple Javascript async each loop
/*
Prevents browsers from crashing with heavy loops
*/
eachAsync = function(array, iterator, callback) {
var timeout = 0;
var counter = 0;
function process() {
iterator.apply && iterator.apply(this, [counter, array[counter]]);
counter += 1;
if (counter < array.length) {
setTimeout(process, timeout);
} else {
callback.apply && callback.apply(this, []);
}
};
setTimeout(process, timeout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment