Skip to content

Instantly share code, notes, and snippets.

@kishmiryan-karlen
Created May 25, 2015 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishmiryan-karlen/3b6f3f80edc49878a335 to your computer and use it in GitHub Desktop.
Save kishmiryan-karlen/3b6f3f80edc49878a335 to your computer and use it in GitHub Desktop.
Simple JS Iterator
function createIterator(items) {
var currentIndex = 0;
return {
next: function() {
if (items[currentIndex]) {
return { value: items[currentIndex], done: false };
} else {
return { value: undefined, done: true };
}
currentIndex++;
}
}
}
// Usage
var iter = createIterator([1, 2, 3]);
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment