Open pfraces opened 8 years ago
var arrayIterator = function (array) {
var length = array.length;
var index = 0;
var iterator = function () {
if (index === length) { return; }
var iteration = { value: array[index], index: index };
index = index + 1;
};
return iterator;
};
var each = function (iteratee, iterator) {
var iteration = null;
while (iteration = iterator()) {
iteratee(iteration.value, iteration.index);
}
};
Async variant
var iterator = arrayIterator([1, 2, 3]);
iterator(function (value, index) {
// ...
});
With this variant it makes no sense to implement an each
method for iterators since the iterator
itself is enough
It supports both, sync and async variants and the standard array/object iterators can get an additional parameters like async: true
or the number of iterations in a tick...
Iterators are functions which iterate data returning each iteration as
{ value, index }
(beingindex
optional) each time is called andundefined
when no more iterations are available