Skip to content

Instantly share code, notes, and snippets.

@jarek-foksa
Created July 22, 2014 20:39
Show Gist options
  • Save jarek-foksa/9211a365732f570c19ec to your computer and use it in GitHub Desktop.
Save jarek-foksa/9211a365732f570c19ec to your computer and use it in GitHub Desktop.
// @info
// Asynchronously iterate over each item of an array or an array-like object.
// The order in which items are iterated is guaranteed to match array order.
var iterateSeries = function (array, iterator, callback) {
array = Array.prototype.slice.call(array, 0);
if (array.length === 0) {
if (callback) callback();
}
else {
var index = 0;
var error = function(error) {
if (callback) callback(error);
};
var next = function() {
index += 1;
if (index >= array.length) {
if (callback) callback();
}
else {
iterator(array[index], once(next), once(error));
}
};
iterator(array[index], once(next), once(error));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment