TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
yield* operator delegates the execution of a generator to another iterable object or generator. When encountered, it pauses the current generator and yields values from the target iterable one by one until the target is exhausted, effectively flattening the iteration process.
Syntax
expression: Any object that implements the iterable protocol (e.g., Generators, Arrays, Strings, Maps, Sets, or custom iterables containing a[Symbol.iterator]()method).
Execution Mechanics
When the JavaScript engine evaluates ayield* expression, it performs the following sequence:
- Iterator Instantiation: It calls the
[Symbol.iterator]()method on the providedexpressionto retrieve an iterator. - Value Delegation: It repeatedly calls the
next()method on the delegated iterator. For everyIteratorResultobject returned withdone: false, the JavaScript engine extracts thevalueproperty and constructs a brand newIteratorResultobject to pass to the caller of the outer generator. Any custom properties attached to the inner iterator’sIteratorResultobject are lost and not passed to the caller. - Expression Evaluation: When the delegated iterator is exhausted (returns an
IteratorResultwheredone: true), theyield*expression evaluates to thevalueproperty of that finalIteratorResult.
Return Value Behavior
A critical mechanical distinction betweenyield and yield* is how the expression itself evaluates within the generator body. While a standard yield evaluates to the argument passed to the outer generator’s next(val) method, a yield* expression evaluates to the explicit return value of the delegated iterator.
Method Proxying
Theyield* operator does not merely loop over values; it acts as a two-way proxy between the outer generator and the delegated iterator.
If the caller invokes next(), throw(), or return() on the outer generator while a yield* expression is active, those method calls (and their arguments) are forwarded directly to the delegated iterator:
next(value): Thevalueis passed to the inner iterator’snext()method.throw(error): If the inner iterator has athrow()method, it is called with theerror. If it does not have athrow()method, the JavaScript engine throws aTypeError(e.g., “The iterator does not provide a ‘throw’ method”), the inner iterator is closed, and the originalerrorpassed to the outer generator is discarded.return(value): If the inner iterator has areturn()method, it is called. TheIteratorResultreturned by the inner iterator’sreturn()method dictates the final return value of the outer generator. This means the inner iterator can override thevalueoriginally passed to the outer generator’sreturn()call.
Delegation to Built-in Iterables
Becauseyield* operates on the iterable protocol, it natively unpacks standard JavaScript data structures without requiring an intermediate generator function.
Master JavaScript with Deep Grasping Methodology!Learn More





