Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active November 27, 2015 18:51
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 JoshCheek/1537bb4ea2a9d5d5298a to your computer and use it in GitHub Desktop.
Save JoshCheek/1537bb4ea2a9d5d5298a to your computer and use it in GitHub Desktop.
Wrote an iterator with Steve Kinney at a JS meetup in Denver (following along with the material at http://thedrearlight.com/bwdid-iterables/#/)
let MyIterator = function(n) {
this.n = n;
}
MyIterator.prototype[Symbol.iterator] = function() {
var i = -1;
var n = this.n;
var done = false;
return {
next: (something) => {
if(done) {
return {next: undefined, done: done}
} else {
done = ++i == n;
return {value: i, done: done};
}
},
throw: (something)=> { /* idk */ },
return: (something)=> { /* idk */ },
}
}
let iter = new MyIterator(4);
for(let val of iter) {
console.log(val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment