Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Async iterators #53

Closed
benjamingr opened this issue Dec 31, 2016 · 4 comments
Closed

Async iterators #53

benjamingr opened this issue Dec 31, 2016 · 4 comments

Comments

@benjamingr
Copy link
Member

Hey,

Async iterators are a stage 3 proposal for asynchronous pull streams (link) with a v8 implementation arriving very soon.

Async iterators and the new for await... of loop are coming our way. I'd like to bring that up for discussion for several reasons.

  • This is likely the last chance Node has to have a say on that spec. Personally I've went through the spec with Node's interests in mind and while some things (like postmortem) are blocked on promise tooling the spec looks very clean and usable to me. I think it'll make users' lives easier.
  • A new Symbol.asyncIterator is introduced. I believe stream.Readable should support Symbol.asyncIteator which would allow it to be iterated with for...await. Note that since async iterators are pull streams backpressure sounds fairly easy to do. cc @nodejs/streams
@benjamingr
Copy link
Member Author

As an example of what asyncIterator might behave like, you could iterate on data events. For example here's a classic "reading data from a request naive example".

let body = '';
for await(const data on req) body += data;
const parsed = JSON.parse(body); // note that we can let errors propagate, since we're in an async fn
console.log("got", parsed);

@mcollina
Copy link
Member

mcollina commented Dec 31, 2016

@benjamingr as recent events played for the multiple inheritance, I have nothing opposing Symbol.asyncIterator where it is available. However, it should not impact performance of existing code in any way, as meta-programming with Symbol recently slowed instanceof of 100x in node v7.

From an implementation perspective, there are probably some lines of code involved.

However, is this a CTC issue? Can we work on this through issues/PR?
@benjamingr it would be good if you could pass by the next stream wg meeting, nodejs/readable-stream#252. Maybe we can open an issue there and we can start discussing it before.

@benjamingr
Copy link
Member Author

However, it should not impact performance of existing code in any way, as meta-programming with Symbol recently slowed instanceof of 100x in node v7.

This would just mean adding a Symbol.asyncIterator to the prototype and should not incur a performance penalty. I have high hopes that eventually it will be fast enough so it can be used in place of .on('data' in "tight" scenarios.

I'll open a separate issue for Symbol.asyncIterator in the readable-stream repo. This issue was more about giving the CTC and collaborators a chance to discuss the feature as a whole before it is "forced upon us" like promises were (so if there is a big objection to the async iteration spec conceptually - we will catch it in time). I apologize if I made it sound as if this issue was focused on Symbol.asyncIterator.

@benjamingr
Copy link
Member Author

This has been addressed in nodejs/readable-stream. Thanks everyone!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants