15

The documentation says that "You can also provide a parameter to the next method to send a value to the generator." Where does it sends it to?

For example, take these 3 generators:

function* one() {
  while(true) {
    var value = yield null;
  }
}
var g1 = one();  g1.next();
g1.next(1000); //yields null

function* two() {
  var i = 0;
  while (true) {
    i += yield i;
  }
}
var g2 = two(); g2.next(); 
g2.next(1000) // yields 1000

function* three(){
  var index = 0;
  while (true)
    yield index++;
}

 g3 = three();
 g3.next();
 g3.next(1000); // yields 1

In generators 3 and 1, the argument passed has no effect on next. Why is that? How does generator 2 calculates its return value? Why it is affected by the given argument?

12
  • the passed in value appears to be returned from the yield. jsfiddle.net/ret4zbsk 0 += 1000 = 1000
    – Kevin B
    May 20, 2016 at 19:33
  • @KevinB Mmm, I still don't understand why doesn't the third generator returns 1001...
    – Federico
    May 20, 2016 at 19:42
  • misread. basically, index++ iterates after, not before. try ++index
    – Kevin B
    May 20, 2016 at 19:47
  • dunno, i still get 1,2,3 from that. however, if i add index = yield index++, i do get differing results. The key seems to be that you update the variable with the yield.
    – Kevin B
    May 20, 2016 at 19:49
  • 2
    Ah, i get it. basically, what you're passing in is being returned from the previous yield, so that the next processing up until the next yield can use it to modify the result. For example, jsfiddle.net/zuasLu9s/3 Since there was no previous yield before the first call to .next(), there's nothing to return to so it just ignores that first value. If you don't accept what is being returned (such as your three generator above) the passed in value won't have any effect. Here's a simpler example: jsfiddle.net/7zgyv1a6/1
    – Kevin B
    May 20, 2016 at 20:23

2 Answers 2

28

The key to understanding this is knowing how the next function retrieves the argument passed to next(), which is as the return value of the yield operator:

[rv] = yield [expression];

Independently of the value of [expression], yield will assign to rv the value passed to next().

But, here comes the tricky part: yield will only assign the value passed to next() when resuming execution from a previous iteration. As a consequence, on the first iteration, yield does not assign anything to rv.

For example, if I have this generator:

function* gen() {
  // On the first iteration, yield does not return anything.
  //because it returns something ONLY when execution is resumed
  returnedFromYield = yield 'foo'; 
  yield returnedFromYield; 
}

returnedFromYield is undefined on the first iteration. When execution is resumed on the second iteration, yield assigns the passed value to the returnedFromYield variable, which is then returned:

g.next(1); // 'foo'
g.next(2); // 2

Let's review another example:

function* gen() {
  yield yield yield 5;
}

On the first iteration, (g.next()), yield will return 5, on the second iteration, (g.next(10)) yield is going to pass 10 to the second yield. That is, yield yield yield 5; on the second iteration is equivalent to yield yield 10;, and, on the third iteration, it's equivalent to yield valuePassedToNext.

5

Here's an annotated example showing the logical flow and scope for the values passed to a generator's next method. The affected area of the next method call is highlighted in the corresponding color.

enter image description here

a: There is no yield, so argument 'one' is ignored. The generator pauses on line 5, returning the value 1.

b: Execution resumes and 'two' replaces yield on line 5, and is assigned to the yieldValue variable. Line 6 is executed. The generator pauses on line 8, returning the value 2.

c: Execution resumes and 'three' replaces yield on line 8, and is assigned to the yieldValue variable. Line 9 is executed. The generator pauses on line 11, returning the value 3.

d: Execution resumes and 'four' replaces yield on line 11, and is assigned to the yieldValue variable. Line 12 is executed. Line 14 is executed. Finally the generator is now done, returning the value undefined.

See the logs on line 28-36

2
  • 1
    Thank you @Christopher. I was so much confused, Your example helped a lot :D Apr 24, 2022 at 16:05
  • You're so welcome @sachinyadav :) I'm glad I could help! Apr 28, 2022 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.