Generator return() and exceptions

Jeremy Martin jmar777 at gmail.com
Wed Jul 23 11:21:11 PDT 2014


>
> Now that we have return() it isn't clear to me that we actually need
> throw()


Am I understanding correctly that you're suggesting removing throw from
Generator.prototype?

If so, I'd like to suggest that, at a minimum, .throw() has been rather
useful in the context of Node.js control-flow libraries [1] that surface
"catchable" asynchronous errors.

[1] https://github.com/jmar777/suspend#error-handling


On Wed, Jul 23, 2014 at 1:52 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>
wrote:

>
> On Jul 23, 2014, at 9:51 AM, Andy Wingo wrote:
>
> On Wed 23 Jul 2014 18:19, Allen Wirfs-Brock <allen at wirfs-brock.com>
> writes:
>
> On Jul 23, 2014, at 1:25 AM, Andy Wingo wrote:
>
>
>    The TC39 notes do not record any discussion of return() causing an
>
>    exception to be thrown.
>
>
>
> In the latest ES6 draft for-of propagates any exceptions thrown by the
>
> call to return(). See
>
> http://people.mozilla.org/~jorendorff/es6-draft.html#sec-runtime-semantics-
>
> forin-div-ofbodyevaluation step 3.k.ii.1-2
>
>
> As a matter of design policy we rarely, if ever, just drop exceptions.
>
>
> I probably didn't explain myself completely; apologies.  I meant that
> the mechanism of iter.return() should be implemented by throwing an
> exception (i.e., as if by "iter.throw(new StopIteration)") instead of
> "returning" from the yield point.
>
>
> Well, in ES6 we don't define iterator termination in terms of a
> StopIteration exception.
>
> However, both gen.throw and gen.return are specified in terms of abnormal
> completion of a yield:
>
> https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generator.prototype.return
>
>
> https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generator.prototype.throw
>
> so they both unwind in a similar manner.
>
> consider:
>
> function gen() {
>     try {
>        while (true) {
>            try {yield foo()
>            } catch (e) {
>               console.log("throw");
>               throw (e);
>            };
>         }
>     } finally {
>       console.log("unwind");
>     };
> }
>
> var  g = gen();
> g.next();
> g.throw(new Error);  //logs: throw, unwind
> var h = gen();
> h.next();
> h.return(); //logs: unwind
>
> Now that we have return() it isn't clear to me that we actually need
> throw() or whether for-of/yield* should call throw() like they currently do.
>
> This is something I hope to discuss at the upcoming TC39 meeting.
>
> Allen
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>


-- 
Jeremy Martin
661.312.3853
http://devsmash.com
@jmar777
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140723/1ac99bf4/attachment.html>


More information about the es-discuss mailing list