-
-
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
This file contains 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* counter() { | |
var count = 0; | |
while (true) { | |
yield count++; | |
} | |
} |
This file contains 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 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