Forwarding `return()` in generators

Axel Rauschmayer axel at rauschma.de
Wed Mar 25 16:40:57 UTC 2015


>> But: you need to guard against other ways of reaching `finally`.
> 
> Why? I intended to always reach finally, and always close the iterator. Also, simplicity ftw :-)
> 
> > That’s not what all the other constructs in ES6 do: they only call `return()` if iteration stops abruptly.
> 
> Only because all the other constructs in ES6 try to exhaust the iterator. And when it's finished anyway, one doesn't need to close it.
> There is in fact no problem with calling `.return()` too often, it just doesn't do anything to completed generators.

True. I hadn’t thought of that.

> Btw, your `take` function is the perfect example where a non-exhausted iterator *should* be closed as return prematurely - like a `break` in a for-of loop would.

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

-- 
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/20150325/c0b81ed0/attachment.html>


More information about the es-discuss mailing list