function*
creates a binding of a new generator function to a given name
function* generator(i) { yield i; yield i + 10; } const gen = generator(10); console.log(gen.next().value); // 10 console.log(gen.next().value); // 20 console.log(gen.next().value); // undefined
A function* declaration creates a GeneratorFunction object
next()
method is called, the generator function's body is executed until the first yield
expression, which specifies the value to be returned from the iterator
or, with yield*
, delegates to another generator function.next()
method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value, as a boolean.next()
method with an argument will resume the generator function execution, replacing the yield expression where an execution was paused with the argument from next()
.function* anotherGenerator(i) { yield i + 1; yield i + 2; yield i + 3; } function* generator(i) { yield i; yield* anotherGenerator(i); yield i + 10; } const 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 console.log(gen.next().value); // undefined