Forwarding `return()` in generators

Axel Rauschmayer axel at rauschma.de
Thu Mar 26 01:20:53 UTC 2015


>> Right. Always closing is much simpler. Otherwise, you’d have to check whether everything was exhausted or not.
> 
> This is by design, FWIW.

Which is OK, I’m more worried about generators behaving differently in reaction to `return()` than, e.g., array iterators. With the latter, you can continue iterating if you break from a `for-of` loop, with the former, you (generally) can’t. Either behavior is OK, but it should be consistent: Is `return()` for optional clean-up (array iterators) or does it really close an iterator (generators)?

The way that `return()` is handled in generators (via try-catch) means that the clean-up action is called in both cases: generator exhaustion and generator closure. If you don’t use a generator then a clean-up action in `return()` is only called if the iterator is closed, not if it is exhausted. Obviously that is a minor technical detail and easy to fix (call the clean-up action when you return `{ done: true }`), but it’s still an inconsistency (which wouldn’t exist if `return()` was called in either case).

-- 
Dr. Axel Rauschmayer
axel at rauschma.de
rauschma.de



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150326/0563c95d/attachment.html>


More information about the es-discuss mailing list