Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator.prototype.next bug (resolved, non-issue) #254

Closed
perchingeagle opened this issue Jul 25, 2016 · 4 comments
Closed

Generator.prototype.next bug (resolved, non-issue) #254

perchingeagle opened this issue Jul 25, 2016 · 4 comments

Comments

@perchingeagle
Copy link

perchingeagle commented Jul 25, 2016

comment deleted, see the next comment

@perchingeagle
Copy link
Author

perchingeagle commented Jul 25, 2016

I found what I think is a problem with generator.prototype.next() and yield, the code below explains my problem:

Generator function

function* gen(){                 // line 1
var rv = (yield 1) + 3;          // line 2
console.log('rv = '+rv);         // line 3
yield 2;                         // line 4
}

Code one (logical code, but does not work)

var iter = gen();
console.log(iter.next(5));
console.log(iter.next()); 

Output

1                        //normal
rv = undefined           //abnormal: expected 'rv = 8'
2                        //normal

The workaround (I think the logic is twisted [no insult])
Code two (workaround, works but not logical)

var iter = gen();
console.log(iter.next());
console.log(iter.next(5));

Output (desired result)

1
rv = 8
2

Why was the variable rv (in line 2) in the generator function set only after the next yield (in line 4) was called?
The value that was sent back using the second call to iter.prototype.next() (that returned line 4's value) was then evaluated (in line 3) to assign a value to the first. (Are you already confused like me, now imagine having to do this with a complex code)
I know Python and Javascript are different languages, but the first code would work just fine in Python:

  1. Line 2 would yield it value (in this case 1).
  2. The first call to next will send back its argument (in this case 5) to the generator and return the first yielded value (1) from the generator.
  3. Line 3 then executes with variable rv (5 + 3 = 8) already set.
  4. Line 4 then yields its value (in this case 2).

(everything is in logical order this way)

In my opinion, when the state is SUSPENDEDSTART just like SUSPENDEDYIELD the value passed to generator.prototype.next() as in next(arg), should not be discarded, it solves that nasty problem.

@fkling
Copy link
Contributor

fkling commented Jul 25, 2016

This is not a bug in regenerator, this is how generators currently work in JS. That's the reason for the proposal of function.sent:

However, the first next that a generator's consumer invokes to start a generator object does not correspond to any yield within the body of the generator function. Instead, the first next simply causes execution of the generator function body to begin at the top of the body.

Because there the first next call does not correspond to a yield within the generator function body there is currently no way for the code with the body to access the initial next argument.

@perchingeagle
Copy link
Author

perchingeagle commented Jul 25, 2016

Thanks, I tried to do the same with Python, and I realized that Python uses
send( ) to solve that little confusion.

On Monday, July 25, 2016, Felix Kling notifications@github.com wrote:

This is not a bug in regenerator, this is how generators currently work in
JS. That's the reason for the proposal of function.sent
https://github.com/allenwb/ESideas/blob/master/Generator%20metaproperty.md
:

However, the first next that a generator's consumer invokes to start a
generator object does not correspond to any yield within the body of the
generator function. Instead, the first next simply causes execution of
the generator function body to begin at the top of the body.

Because there the first next call does not correspond to a yield within
the generator function body there is currently no way for the code with the
body to access the initial next argument.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#254 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AExc1ZvRgi2-nJ9rLfnmH-LyhgsmSkCVks5qZE5dgaJpZM4JTwxE
.

@perchingeagle perchingeagle changed the title Sandbox bug report Generator.prototype.next bug (resolved, non-issue) Jul 25, 2016
@benjamn
Copy link
Collaborator

benjamn commented Nov 30, 2016

Thanks for digging into this.

@benjamn benjamn closed this as completed Nov 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants