Created
July 22, 2014 20:39
-
-
Save jarek-foksa/9211a365732f570c19ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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