Created
November 15, 2017 18:42
-
-
Save bbaaxx/fb800ecffdad217b545d753a73609ed9 to your computer and use it in GitHub Desktop.
Code for blog post about iterators iterables and generators with ES6
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
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