-
Notifications
You must be signed in to change notification settings - Fork 23
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 functions work flow" API page not exactly correct #339
Comments
I forgot that page existed. You are correct, function *doStuff(iterator) {
let abrupt = true;
try {
while(!iterator.next().done) { /*...*/ }
abrupt = false;
} finally {
if (abrupt) iterator.return();
}
} |
Is it? It's not mentioned in MDN's iteration protocols (nor any related page). I'm trying to read ECMA-262 but I'm not getting anywhere for now. |
Found it. https://exploringjs.com/es6/ch_iteration.html#sec_iteration-protocol-in-depth indeed lists Feel free to close the issue (or leave it open if you intend to update the wiki page). EDIT: related from the book:
There's a whole section about closing iterators. I was interested in the updated wiki page to refer colleagues to it, but that book is a must! |
I came across https://github.com/iter-tools/iter-tools/wiki/Generator-functions-work-flow via bustle/streaming-iterables#22 which was a pretty interesting detail I didn't know, but the example didn't really make sense to me (the finally is going to get called anyways), so I tested the actual implementation.
Here's a counterexample:
This outputs:
Notice that:
.return()
callSince
return()
is a method of Generator and not part of iterable nor iterator protocols, does it make sense to support the fake'sreturn()
? It's not part of any public interface and in fact I wouldn't expect to get myreturn()
called if I implemented it by coincidence, but both Node and Firefox call it 😕In the real generator the finally block is going to get called anyways. I guess the key takeaway is to call
return()
, but not as in the wiki example, but only if you break early, i.e. the wiki page breaking condition shouldn't benext.done
, at least if we aim to conform tofor ... of
behavior (which I still think is pretty surprising and not mentioned anywhere in MDN... I'm searching the actual spec but it's kinda hard to find).Anything I'm missing? What do you think?
The text was updated successfully, but these errors were encountered: