Skip to content

Instantly share code, notes, and snippets.

@dtao
Created April 10, 2012 14:52
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save dtao/2351944 to your computer and use it in GitHub Desktop.
Save dtao/2351944 to your computer and use it in GitHub Desktop.
Function to asynchronously iterate over a collection
function eachAsync(collection, iterator, callback) {
var iterate = function(i) {
setTimeout(function() {
iterator(collection[i]);
if (i < collection.length) {
iterate(i + 1);
} else {
callback();
}
}, 0);
};
iterate(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment