News for hackers. Stuff that matters.
Re:ES6 generators in depth by agentultra on Monday March 30, 2015 @10:42AM (#9289801)
A very in-depth post!

As Python has had generators for years already there is plenty of resources out there for the curious that translate well to ES6[0].

[0] http://www.dabeaz.com/coroutines/

Re:ES6 generators in depth by rauschma on Monday March 30, 2015 @11:06AM (#9289980)
His work is great! Linked to from the blog post...
Re:ES6 generators in depth by callum85 on Monday March 30, 2015 @01:32PM (#9291061)
Here is another deep explanation of generators, by Kyle Simpson:

https://github.com/getify/You-Dont-Know-JS/blob/master/async...

Both of them are really good, and come at it from slightly different angles.

Re:ES6 generators in depth by jakerella86 on Monday March 30, 2015 @02:31PM (#9291526)
And if you follow them on Twitter there are some good conversations back and forth. ;)
Re:ES6 generators in depth by shawndumas on Monday March 30, 2015 @10:54AM (#9289889)
There is a GitHub repo too --> https://github.com/rauschma/generator-examples
Re:ES6 generators in depth by NathanKP on Monday March 30, 2015 @03:04PM (#9291787)
I want to like generators for asynchronous code, but the one thing that disappoints me about generators is how awkward it is to write code that does the same as an async.auto

I use async.auto because of the ease of adding new asynchronous functions that depend on one or more other asynchronous functions having been executed beforehand.

Simply put you can break up a task into steps that depend on each other, and the steps run whenever all their dependencies have been executed. You don't have to worry about whether the functions run in series, or parallel, you just define that one function depends on another and it just works.

I have yet to see a promise example, or a generator example that had the combination of maintainability and legibility that an async.auto has. What I really want to see is a reasonably complicated task that has say 8 or more asynchronous steps mocked out in generators. Say something like:

Post a social item

1) Write social item to database

2) Write reference to social item in feed of person who posted item

3) Fetch friends of user who posted item

4) Fetch notification settings of each friend

5) Add a reference to the social item to each friend's personal activity stream

6) Based on notification settings of each user send an email alerting each friend of the event

7) Invalidate activity stream cache key for poster, and friends

In this flow:

1 & 3 can run in parallel at the start as they don't depend on anything

2 & 5 can run as soon as 1 is finished as they depend on the ID of the object in #1

4 & 5 can run as soon as 3 is finished as they depend on the friend list

6 can run as soon as 4 is done as it needs to know who to email and who not to email

7 can run as soon as 2 & 5 are done as the streams must be updated before the cache can be cleared

With async.auto it is easy to create 7 async functions and define the dependencies, then sit back and watch it run efficiently and magically. Later if you need to add more actions it is easy to insert another function, define what it depends on and see it working.

I still don't understand how to do something like this with generators, and none of the examples I've seen so far is complex enough, nor do they seem to solve this problem in a format as clean as async.auto

Re:ES6 generators in depth by Strilanc on Monday March 30, 2015 @12:37PM (#9290678)
I didn't realize they doubled as both iterables and observables. Being able to pass a value in is a useful trick, although it looks a bit annoying to forward passed in values *due to error/return requiring try-catch shenanigans).
Re:ES6 generators in depth by cschneid on Monday March 30, 2015 @11:18AM (#9290075)
Amazingly in depth post!

I'm sad when programming languages have to reach for new keywords and syntax to add features.

`function*` is difficult to google for, and doesn't have any human readable hints for somebody to understand.

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?
Re:ES6 generators in depth by callum85 on Monday March 30, 2015 @01:31PM (#9291055)
I don't think googling is much of an issue here. Unless you're talking about the very first occasion that someone sees "function*", then OK, they can't google for that easily... But they can google "function with astersik" or something and they'll find out it's called a "generator", and use that in future.
Re:ES6 generators in depth by rjbwork on Tuesday March 31, 2015 @03:02AM (#9294763)
An unpopular opinion on a post about JavaScript maybe, but C#, Python, and others have implemented generators with no extra syntax because of an actual type system aside from "object, number, string, bool".