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 keyword is an operator used exclusively within generator functions (function*) and asynchronous generator functions (async function*) to pause the execution context and yield a value to the caller. It acts as a bidirectional control-flow mechanism, allowing a generator to emit a sequence of values over time and receive injected values or exceptions when execution is resumed.
Syntax
expression: The value to be evaluated and yielded to the caller via the iterator protocol. If omitted, the generator yieldsundefined.rv: The value theyieldexpression evaluates to inside the generator. This is determined by the argument passed back into the generator when the caller invokes the generator’snext()method.
Execution Mechanics
When the JavaScript engine evaluates ayield expression, the following sequence occurs:
- Evaluation: The
expressionfollowing theyieldkeyword is evaluated. - Suspension: The generator’s execution context (local variables, lexical scope, and instruction pointer) is frozen in memory.
- Emission: The evaluated expression is wrapped in an
IteratorResultobject shaped as{ value: <expression_result>, done: false }. For synchronous generators, this object is yielded directly to the caller. For asynchronous generators, the caller receives aPromisethat resolves to thisIteratorResultobject. - Resumption: The generator remains suspended until the caller invokes one of the iterator’s control methods:
next(),throw(), orreturn(). - Injection/Termination:
- If
next(val)is invoked,valbecomes the evaluated result of the entireyieldexpression in the resumed execution context. - If
throw(err)is invoked, the generator resumes by throwing theerrexception directly at the location of the suspendedyieldexpression. - If
return(val)is invoked, the generator immediately terminates, executing any pendingfinallyblocks, and returns{ value: val, done: true }.
- If
Operator Precedence
yield is a right-associative operator with very low precedence (equivalent to assignment operators). Because of this low precedence, an expression like yield 1 + 2 is perfectly valid and evaluates as yield (1 + 2).
However, when a yield expression is used as an operand for an operator with higher precedence, the yield expression must be wrapped in parentheses. Failing to do so results in a SyntaxError. For example, the grammar rules for the addition operator (+) require a higher-precedence expression (a MultiplicativeExpression) on its right side. Because yield 2 is an AssignmentExpression (lower precedence), the parser fails to match the grammar and throws an error.
Yield Delegation (yield*)
The yield* expression is a specialized variant that delegates iteration control to another iterable object (such as another generator, an Array, a Map, or a String).
Instead of yielding the iterable object itself, yield* iterates over the target and yields each of its values sequentially. The yield* expression itself evaluates to the return value of the delegated iterator (the value when done: true).
Master JavaScript with Deep Grasping Methodology!Learn More





