Where argument of first next() call goes? [duplicate]

The next method is defined as follows:

25.3.1.2 Generator.prototype.next ( value )

The next method performs the following steps:

  1. Let g be the this value.
  2. Return GeneratorResume(g, value).

The GeneratorResume abstract operation uses value at step 10:

25.3.3.3 GeneratorResume ( generator, value )

The abstract operation GeneratorResume with arguments generator and
value performs the following steps:

  1. Let genContext be the value of generator’s [[GeneratorContext]] internal slot.
  1. Resume the suspended evaluation of genContext using NormalCompletion(value) as the result of the operation that
    suspended it. Let result be the value returned by the resumed
    computation.

The first possibility is that the evaluations was suspended by the use of yield (i.e. "suspendedYield" state).

Its behavior is explained in 14.4.14 Runtime Semantics: Evaluation:

YieldExpression : yield

  1. Return GeneratorYield(CreateIterResultObject(undefined,
    false)).

(Analogous for YieldExpression : yield AssignmentExpression)

The GeneratorYield abstract operation suspends the generator as follows:

  1. Set the code evaluation state of genContext such that when evaluation is resumed with a Completion resumptionValue the
    following steps will be performed:

    1. Return resumptionValue.
    2. NOTE: This returns to the evaluation of the YieldExpression production that originally called this abstract operation.

So the value passed as the argument of the second next is used as the returned value of the first yield expression. And the value passed as the argument of the 3rd next is used as the returned value of the 2nd yield expression. And so on.

However, there is also the possibility that the generator hadn’t started yet (i.e. "suspendedStart" state).

This is done by the GeneratorStart abstract operation:

  1. Set the code evaluation state of genContext such that when evaluation is resumed for that execution context the following
    steps will be performed:

But those “following steps” don’t use the resumption value.

Therefore, the value passed as the argument of the first next is discarded.

Leave a Comment