Generator Arrow Functions

Ѓорѓи Ќосев gorgi.kosev at gmail.com
Fri Nov 15 09:55:39 PST 2013


On 11/15/2013 06:07 PM, Kevin Smith wrote:
> Besides, with async functions, the use case established in this thread
> for generator arrows basically disappears.  It's probably better not
> to introduce convenience features that will be made obsolete by the
> next version of the language.

Can someone explain how would that use case disappear with "async
functions"?

If by "async functions" you mean something that would allow await [1] -
as far as I understand, this already works with generators:

    function* getAllUserDataLazily() {
        for (const userName of users) {
            yield getUserData(userName);
        }
    }

    function* outerGenerator() {
        for (const dataPromise of getAllUserDataLazily()) {
            console.log(yield dataPromise);
        }
    }

The inner `getAllUserDataLazily` generator yields promise values to the
`outerGenerator`, then pauses. The outer generator is run by the async
task runner and it yields the promise, then it also pauses. The task
runner's code now waits for the promise to resolve. Once the promise
resolves, it resumes the `outerGenerator`. Once the `outerGenerator`
resumes, it goes back to the beginning of the loop and resumes
`getAllUserDataLazily`. Basically, as long as the outermost generator is
run by an async runner, both lazy and async execution of all nested
generators/iterators is possible.

Another benefit is that this works with primitives other than promises.
Current libraries will love that, as not all are based on promises. By
the time ES7 is released, a large ecosystem of async generators will
already be in place. Infact, its already here, right now - and ES6 isn't
even finalized yet. This is a guess, but I'd say that at that point, a
new async functions feature will be seen as unnecessary and async
generators as "good enough". IMO, the cat is already out of the bag.

[1]: https://gist.github.com/domenic/6656283
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131115/e5cde5d7/attachment.html>


More information about the es-discuss mailing list