-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Symbol.asyncIterator
#254
Comments
I'd also like to point out I'm far from being a NodeJS streams expert :) |
Note that this idea isn't guaranteed to work :) it could generate fatter code that simply isn't worthwhile. Best case, you'd still have a property load for It's something to try in 2017 though. |
Before adding this to core, I think we should experiment with this in userland, even through the nodejs org. It's simple to polyfill, and I can think 2-3 different ways to implement it. I recommend we experiment with the idea, and come up with a benchmark. This will help everyone assess the situation, before adding something to core that we will have to support forever. I'm happy to produce that impl, if someone can help set it up the whole transpiling chain to try it. |
I think I may have written an implementation of this lemme check |
false alarm, I have not, yet |
though more generally I'd vote for waiting and seeing what whatwg streams do re async iterators before we do anything |
I generally agree. However, we should check if that API works for us before its finalized. After which, we do not have much chance to change things. |
|
@calvinmetcalf sounds good to me! I think we should implement now (maybe behind a flag) and see how it works for us. |
more likely make a separate package to add it for now
…On Fri, Jan 6, 2017 at 11:37 AM Benjamin Gruenbaum ***@***.***> wrote:
@calvinmetcalf <https://github.com/calvinmetcalf> sounds good to me!
I think we should implement now (maybe behind a flag) and see how it works
for us.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#254 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABE4n5JFFqbOfnBmGTQ7NIt28qmNg3L9ks5rPm3ngaJpZM4LYlAl>
.
|
@calvinmetcalf yes. We can prototype it there with transpilers, and see how things are going. Then, import it here or within core. |
prototype version, also this issue was originally nodejs/CTC#53 |
@calvinmetcalf can you upload to NPM? I want to make sure I play with it and have feedback before the meeting. |
I've opened up a couple of issues we have to discuss. |
Closing for now, feel free to reopen @benjamingr if there is something more for us to do! |
I'm pretty content with the resolution and I think we can close this until async iterators land in Node at which point we'll probably want to revisit this and add support to core :) Thanks! |
A use case that will gain from iterable streams: |
Fwiw this is available behind a harmony flag in Node 7+, so if we want to, we can start implementing things in core. |
See also discussion in nodejs/promises#31 regarding optimizing async iterators. I think we should start implementing - but can't expect it to be fast any time soon. |
When will async-iterators be outside of the harmony flag? We have a working prototype https://github.com/calvinmetcalf/async-iter-stream, can you check if it works with |
@mcollina |
@targos this is amazing news. |
I've been asked to send one out recently.
This would give us some useful data for profiling, so the sooner the better :) |
When we ship, I don't mind writing a benchmark for readable streams as async iterators (vs callbacks with event emitters) in a real world app example (opening several streams and reading from there to fulfill a user request). |
@benjamingr i'd be interested to see that, though they are going to have different perf characteristics because async-iterators support back pressure, though any help making async-iter-stream better would be very appreciated, I've got a few issues created in that repo |
@vsemozhetbyt I have just verified that https://github.com/mafintosh/csv-parser is as fast as the python alternative. |
Async iterators are a stage 3 proposal for asynchronous pull streams (link) with a v8 implementation arriving very soon.
A new
Symbol.asyncIterator
is introduced. I believestream.Readable
should supportSymbol.asyncIteator
which would allow it to be iterated withfor...await
.Supporting
Symbol.asyncIterator
means that readable streams are iterable through the newfor await
loops. ref.Meaning we'll be able to do
Semantics
The async iterator protocol looks like:
Which is similar to the regular JavaScript iteration protocol except that it returns promises. Since it is a pull stream supporting back pressure is pretty simple (since the consumer asks for values rather than being pushed values).
You
.next
the async iterator to get a promise for the next value in the iteration. You also can build async iterators with async generators. Here are some examples for both from a talk I gave: https://docs.google.com/presentation/d/1r2V1sLG8JSSk8txiLh4wfTkom-BoOsk52FgPBy8o3RMImplementation
We'll add a
Symbol.asyncIterable
to readable streams that returns an object withnext
throw
andreturn
methods.next
is called by thefor await
loop to get the next value, it expects a promise to be returned that resolves with adone/value
pair. This should likelyreturn
is called when a user abruptly completes the loop by callingbreak
. This is our chance to do cleanup - like what you'd usefinally
for and it marks completion (likedone: true
).throw
is on the consumer's side to propagate errors to the async iterator. I'm not sure what we should do here.With regards to backpressure - the "easiest" thing would be to pause the stream whenever we just gave a value and unpause it on
next
- I'm concerend if that might affect performance even further.Note that while there are interesting problems to solve with async iterators in general - I think our initial focus should be users of the new
for await
loop.Performance
Allocating a promise for each value in
next
sounds prohibitively expensive. However, I am optimistic this will be solved in V8 land and the promise resolution will be inlined.I recommend we start with a simple implementation and work our way towards a more optimized one.
The text was updated successfully, but these errors were encountered: