Do we really need async generators?
Missed the list...
---------- Forwarded message ---------- From: Isiah Meadows <isiahmeadows at gmail.com>
Date: Mon, Mar 13, 2017 at 12:14 AM Subject: Re: Do we really need async generators? To: Axel Rauschmayer <rauschma at icloud.com>
From my basic reading, this is not that far off of how iterators work
without for ... of
. When I don't have those loops available (e.g.
working in ES5 code), I've found this pattern to be really helpful in
interoperating with ES6 collections:
for (var next = iter.next(); !next.done; next = iter.next()) {
// use `next.value`
}
That's very similar to your idea of iterating CSP collections:
for (var next = await channel.take(); next !== Channel.DONE; next =
await channel.take()) {
// use `next`
}
It's also similar to how C++'s iterators worked before C++ 11:
for (std::vector<int>::iterator iter = vec.begin(); iter != vec.end(); iter++) {
// use `*iter`
}
This will get unwieldy in a hurry due to its verbosity, though, and even C++ added a sugar version to abstract this. Hence the async iterator proposal.
I do feel CSP channels are closer to Node streams and observables than async iterators, though, and your proposal seems awfully similar to this. I do feel that your CSP ideas could really carry over with my non-linear control flow strawman, though (Note that I'm about to redo much of that strawman, to clean it up substantially).
Isiah Meadows me at isiahmeadows.com
I’ve thought some more about the async iteration proposal [1] and my thinking has evolved:
for-await-of
useful.I’m genuinely interested in finding the best possible approach, so any kind of feedback is welcome – especially if CSP have any kind of deficiency that I have overlooked.
Details: gist.github.com/rauschma/4dc86ea81585fcfe056de3caa19aa38f, gist.github.com/rauschma/4dc86ea81585fcfe056de3caa19aa38f (I’ll probably publish this blog post on Monday or Tuesday)
[1] tc39/proposal-async-iteration
Axel