News for hackers. Stuff that matters.
Re:ES6 generators in depth by rauschma on Monday March 30, 2015 @11:21AM (#9290102)
JavaScript is constrained by backwards compatibility. That’s why the ideal solution, the keyword `generator`, wasn’t possible.
Re:ES6 generators in depth by callum85 on Monday March 30, 2015 @01:29PM (#9291041)
This can't be about backwards compatibility. If it was, they wouldn't have been able to add 'yield, 'await' etc.

I don't agree the keyword "generator" would have been ideal. It wouldn't work when using 'enhanced object literals' [1], where you can declare a method without even using the keyword "function". In this case, if you want the method to be a generator, you can just put an asterisk in front of its name, so it's consistent with the "function*" syntax.

[1] https://babeljs.io/docs/learn-es6/#enhanced-object-literals

Re:ES6 generators in depth by tlrobinson on Monday March 30, 2015 @01:54PM (#9291252)
FYI, "yield" has been a reserved word since ES5 and "await" is becoming reserved in ES6 (for eventual use in ES7): https://mathiasbynens.be/notes/reserved-keywords

It seems like a word must be reserved for at least one ES version prior to it being used.

Re:ES6 generators in depth by 15155 on Monday March 30, 2015 @01:00PM (#9290841)
What about the "yield" keyword?
Re:ES6 generators in depth by adrusi on Monday March 30, 2015 @01:15PM (#9290937)
It's only valid as a keyword within the `function*` block, so the parser can treat it as an identifier elsewhere. I haven't read the detailed spec, so I'm not sure that this is the standard behavior, but this would be a way to preserve complete backward compatability.
Re:ES6 generators in depth by rauschma on Monday March 30, 2015 @04:04PM (#9292197)
Yes, that’s how it works. `yield` is only reserved in ES5 strict mode. But due to it only being a keyword inside generators, there are no problems in non-strict mode.
Re:ES6 generators in depth by onestone on Monday March 30, 2015 @12:16PM (#9290509)
Why do you think the current syntax is more backwards-compatible? Vanilla ES5 parsers complain loudly when seeing "function *".
Re:ES6 generators in depth by nkron on Monday March 30, 2015 @12:28PM (#9290605)
It's the other way around. ES6 needs to support loading ES5 content. They can't make 'generator' a keyword because it was a perfectly fine variable name in ES5.
Re:ES6 generators in depth by onestone on Monday March 30, 2015 @02:43PM (#9291627)
You are right. My bad.
Re:ES6 generators in depth by tlrobinson on Monday March 30, 2015 @01:56PM (#9291268)
That would be considered "forward-compatible".
Re:ES6 generators in depth by mikewhy on Monday March 30, 2015 @11:40AM (#9290228)
which is strange, considering the `async` keyword in upcoming versions.
Re:ES6 generators in depth by rauschma on Monday March 30, 2015 @04:01PM (#9292177)
AFAICT, `async function` is OK, because you combine a keyword with something that is contextually turned into a keyword. With a single non-keyword that isn’t possble. For example, the following code (a hypothetical ES6 anonymous generator expression) could be an ES5 function call followed by a code block.

    generator (a, b, c) {
        ···
    }
Re:ES6 generators in depth by adrusi on Monday March 30, 2015 @01:19PM (#9290967)
That's why it wasn't included in the ecmascript 6 draft. In es6, "async" and "await" are future-reserved keywords. It's kind of a process of marking variables named "async" as deprecated. The spec authors have to balance compatibility with making the language pleasant to use.
Re:ES6 generators in depth by tlrobinson on Monday March 30, 2015 @01:59PM (#9291301)
Is there even any situation where "async function(..." or "generator function(..." is currently valid syntax?