Skip to content

Instantly share code, notes, and snippets.

@hfknight
Last active January 4, 2017 02:03
Show Gist options
  • Save hfknight/fb706b646272020f68c21abbcdbe3203 to your computer and use it in GitHub Desktop.
Save hfknight/fb706b646272020f68c21abbcdbe3203 to your computer and use it in GitHub Desktop.
function* Generator
/*
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