Last active
September 14, 2016 08:59
-
-
Save bmeck/12e979837b9a73b5f377 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