Last active
January 4, 2017 02:03
-
-
Save hfknight/fb706b646272020f68c21abbcdbe3203 to your computer and use it in GitHub Desktop.
function* Generator
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
/* | |
The function* declaration (function keyword followed by an asterisk) defines a generator function, which returns a Generator object. | |
You can also define generator functions using the GeneratorFunction constructor and a function* expression. | |
*/ | |
// Syntax | |
function* name([param[, param[, ... param]]]) { | |
statements | |
} | |
// Example | |
function* anotherGenerator(i) { | |
yield i + 1; | |
yield i + 2; | |
yield i + 3; | |
} | |
function* generator(i){ | |
yield i; | |
yield* anotherGenerator(i); | |
yield i + 10; | |
} | |
var gen = generator(10); | |
console.log(gen.next().value); // 10 | |
console.log(gen.next().value); // 11 | |
console.log(gen.next().value); // 12 | |
console.log(gen.next().value); // 13 | |
console.log(gen.next().value); // 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment