Proposal: Generator returning a value should throw SyntaxError

Adam Ahmed aahmed at atlassian.com
Mon Jan 27 14:18:49 PST 2014


In light of the recent thread discussing async and await keywords, I
thought it'd be appropriate to raise this point again, understanding it may
be too late to make a change.

As my original post details, the concept of `return` within a generator is
surprising in its difference in behavior from `yield`.

This does not do as 'expected' in a for-in:

function * threeCount() {
  yield 1;
  yield 2;
  return 3;
}

The argument for allowing return values was that usages in the vein of
task.js will use the return value as a real return value and the yields for
scheduling.

If we' re going to have async and await serve the scheduling purpose as
well, can we remove the 'return' foot gun from generators? It sounds like
it's just a stopgap until async-await, and a painful one, IMO. A syntax
error on a generator that returns values would make the scheduling
(async-await) vs iteration (generator) use cases much more clear. It'll be
much easier for new JS devs to understand generators.

Happy to be shutdown again, just thought it was worth reconsidering with
new async-await keywords in play.
On 27/09/2013 3:46 PM, "Brandon Benvie" <bbenvie at mozilla.com> wrote:

> On 9/26/2013 10:40 PM, Brandon Benvie wrote:
>
>> ```js
>> function* foo() {
>>   yield 'what';
>>   yield 'ever';
>>   return "DONE";
>> }
>>
>> function* bar() {
>>   console.log(yield* foo());
>> }
>> ```
>>
>
> Err, this logs "DONE" when you do:
>
> ```js
> var gen = bar();
> gen.next();
> gen.next();
> gen.next();
> ```
> but you got the idea...
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140128/7530bb94/attachment.html>


More information about the es-discuss mailing list