You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
When thenext method is invoked on a generator objects, the value passed as the first argument tonext is "sent" to the generator object and becomes available within the body of the generator function as the value of theyield expression that most recently suspended the generator function. This supports two-way communications between the a generator object and its consumer.
However, the firstnext that a generator's consumer invokes to start a generator object does not correspond to anyyield within the body of the generator function. Instead, the firstnext simply causes execution of the generator function body to begin at the top of the body.
Because there the firstnext call does not correspond to ayield within the generator function body there is currently no way for the code with the body to access the initialnext argument. For example:
function*adder(total=0){letincrement=1;while(true){switch(request=yieldtotal+=increment){caseundefined:break;case"done":returntotal;default:increment=Number(request);}}}lettally=adder();tally.next(0.1);// argument will be ignoredtally.next(0.1);tally.next(0.1);letlast=tally.next("done");console.log(last.value);//1.2 instead of 0.3
In the above example, the argument to thenext method normally supplies the value to added to a running tally. Except that the increment value supplied to the first next is ignored.
This proposal provides an alternative way to access thenext parameter that works on the first and all subsequent invocations of a generator'snext method.
The Proposal
A new meta-property:function.sent
Value and Context
The value offunction.sent within the body of a Generator Function is the value passed to the generator by thenext method that most recently resumed execution of the generator. In particular, referencingfunction.sent prior to the first evaluation of ayield operator returns the argument value passed by thenext call that started evaluation of theGeneratorBody.
function.sent can appear anywhere aYieldExpress would be legal. Referencingfunction.sent outside of aGeneratorBody is a Syntax Error.
Usage Example
Here is how the above example might be rewritten usingfunction.sent
function*adder(total=0){letincrement=1;do{switch(request=function.sent){caseundefined:break;case"done":returntotal;default:increment=Number(request);}yieldtotal+=increment;}while(true)}lettally=adder();tally.next(0.1);// argument no longer ignoredtally.next(0.1);tally.next(0.1);letlast=tally.next("done");console.log(last.value);//0.3
Specification Updates
The following are deltas to the ECMAScript 2015 Language Specification
8.3 Execution Contests
The following row is added toTable 24:
Component
Description
LastYieldValue
The value of the most recently evaluatedYieldExpression
FunctionSent : function . sent 1. Assert: the running execution context is a Generator Context. 2. LetgenContext be the running execution context. 3. Return the value of the LastYieldValue component ofgenContext .
25.3.3.1 GeneratorStart(generator, generatorBody)
Between lines 3 and 4 of the ES6 algorithm add the following step:
3.5. Set the LastYieldValue component ofgenContext toundefined.
25.3.3.3 GeneratorResume(generator, value)
Between lines 8 and 9 of the ES6 algorithm add the following step:
8.5. Set the LastYieldValue component ofgenContext tovalue.
25.3.3.5 GeneratorYield(iterNextObj)
Between lines 5 and 6 of the ES6 algorithm add the following step:
5.5. Set the LastYieldValue component ofgenContext toundefined.