Skip to content

Instantly share code, notes, and snippets.

@bbaaxx
Created November 15, 2017 18:42
Show Gist options
  • Save bbaaxx/fb800ecffdad217b545d753a73609ed9 to your computer and use it in GitHub Desktop.
Save bbaaxx/fb800ecffdad217b545d753a73609ed9 to your computer and use it in GitHub Desktop.
Code for blog post about iterators iterables and generators with ES6
function makeIterator(array) {
let nextIndex = 0;
return {
next: function() {
return nextIndex < array.length ?
{value: array[nextIndex++], done: false} :
{done: true};
}
};
}
const it = makeIterator(['yo', 'lo']);
console.log(it.next().value); // 'yo'
console.log(it.next().value); // 'lo'
console.log(it.next().done); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment