Generators
suggest changeIntroduction
Generator functions, declared with function*, can pause at yield expressions and resume later. Calling one returns a generator object that implements both the iterator and iterable protocols.
Syntax
function* name(parameters) {
yield value;
return finalValue;
}
const generator = name(arguments);
generator.next(value);
generator.return(value);
generator.throw(error);
Remarks
The object returned by next(), return(), or throw() has value and done properties. A generator runs synchronously between suspension points; use an async generator (async function*) when producing values requires asynchronous work.
- Generator functions
- Sending values to a generator
- Delegating to another generator
- Iteration
- Async flow with generators
- Generator iterator interface
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents