Skip to content

Instantly share code, notes, and snippets.

@devmobasa
Last active April 26, 2017 04:57
Show Gist options
  • Select an option

  • Save devmobasa/7f81dc14eea77d3d2b02aba114aed862 to your computer and use it in GitHub Desktop.

Select an option

Save devmobasa/7f81dc14eea77d3d2b02aba114aed862 to your computer and use it in GitHub Desktop.
JavaScript Iterators - Coding Blast - www.codingblast.com
function makeIterator(array) {
let index = 0;
return {
next: function () {
if (index < array.length) {
return { value: array[index++], done: false };
} else {
return { done: true };
}
}
};
}
let it = makeIterator(['coding', 'blast']);
console.log(it.next()); // [object Object] { done: false, value: "coding" }
console.log(it.next()); // [object Object] { done: false, value: "blast" }
console.log(it.next()); // [object Object] { done: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment