Skip to content

Instantly share code, notes, and snippets.

@Sysetup
Created October 13, 2016 00:14
Show Gist options
  • Select an option

  • Save Sysetup/8e24962836e2b69645b0ec00040c4ca7 to your computer and use it in GitHub Desktop.

Select an option

Save Sysetup/8e24962836e2b69645b0ec00040c4ca7 to your computer and use it in GitHub Desktop.
Iterators basic example.
function makeIterator(){
var nextIndex = 0;
return {
next: function(){
return nextIndex < 5 ? { value: nextIndex++, done: false} : {done: true}
}
}
}
var it = makeIterator();
console.log('Value: '+ it.next().value);
console.log('Value: '+ it.next().value);
console.log('Done: ' + it.next().done);
console.log('Value: '+ it.next().value);
console.log('Value: '+ it.next().value);
console.log('Value: '+ it.next().value);
console.log('Done: ' + it.next().done);
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment