Skip to content

Instantly share code, notes, and snippets.

@james-gardner
Forked from bmeck/counter-generator.js
Created September 14, 2016 08:59
Show Gist options
  • Save james-gardner/dc385cecf9d8f3ec7b8e30110a58c6da to your computer and use it in GitHub Desktop.
Save james-gardner/dc385cecf9d8f3ec7b8e30110a58c6da to your computer and use it in GitHub Desktop.
how a counter generator in es6 maps to a function in es5
function* counter() {
var count = 0;
while (true) {
yield count++;
}
}
function counter() {
var done = false;
var count = 0;
return {
next: function (value) {
if (done) throw new Error('Generator is done');
return {done: done, value: count++};
},
throw: function (err) {
done = true;
throw err;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment