-
Notifications
You must be signed in to change notification settings - Fork 37
NodeJS Stream Meeting Notes - Async Iterators #74
Comments
Thanks @benjamingr. I hope this will clarify things further. Node.js Streams are composed of three types/classes:
If we are not interested in controlling the amount of data (backpressure) that the Node.js process accepts, we can use the stream.on('data', function (chunk) {
// do something with chunk
})
stream.on('end', function () {
// data finished
}) If we are interested in controlling the amount of data that the Node.js process accepts, we can use readable.pipe(transform).pipe(writable) We can also concatenate multiple transforms: readable.pipe(transform1).pipe(transform2).pipe(writable) AsyncIterator is another way we can interact with streams, without using In @calvinmetcalf prototype, we can do: async function test() {
for await (let x of new MyStream()) {
console.log(x);
}
}
test().then(()=>console.log('done'), e=>{console.log(e)}); This allows us to read from a stream just using a language feature. However it is not clear how we could Ideally we should be able to do: async function doubleStream (iter) {
for await (let x of iter) {
yield x * 2
}
}
async function toDest (iter, dest) {
for await (let x of iter) {
await dest.write(s)
}
await dest.end()
}
toDest(doubleStream(doubleStream(stream)), writable) Error handling was also a topic in the meeting, but it is not clear what it happens if something inside the |
Would it make sense to allow async function *messagesFromSomewhere(somewhere){
// etc...
}
var messages = messagesFromSomewhere("here");
fs.createWriteStream("messages.txt").write(messages); This would make it easy to consume sequences with streams, and I think would be intuitive to use without breaking backwards compatibility. |
async function *messagesFromSomewhere(somewhere){
// etc...
}
var source = Readable.wrap(messagesFromSomewhere("here"))
var dest = fs.createWriteStream("messages.txt")
source.pipe(dest) Can I know if |
yes the promise result includes a done member, having the ability to create a readable from an async iterator seams like a good idea |
Would it make sense to call it Would it make sense to allow regular iterables like Array or Set, then? This would potentially help get rid of the need for user libraries like stream-array. |
we'd probably need it to be more like the Readable.prototype.wrap so that options could be sepcified (i.e. objectMode, highwatermark and encoding) |
@RangerMauve I agree. However it's not the point of the discussion, we can have that in our readable-stream repo. I think this issue is only about async-iterator, whatever the method name is, we need to make sure can wrap them easily, and possibly assemble them in a transform pipeline. @calvinmetcalf do you know how would you implement |
yes I'll open a pull for async-iter |
This is all very interesting; it's really great to hear that async iteration may fit seemlessly with Node Streams. Do you have any feedback that could affect the semantics of this proposal? Do the currently proposed semantics work well for Node? |
I've been following this spec for a while and am a big fan and think it's a great fit. |
I didn't know about it, and I'm really happy with the spec. |
What is the officially recommended way for using for-await-of with streams on Node.js at the moment? An npm package such as async-iterate-stream? |
Hey,
Node discussed
Symbol.asyncIterator
streams in the last WG meeting yesterday (I had to miss it at the last minute :( ).One issue raised was how to make async iterators work with writable streams - that is, there needs to be a way to do:
And have that coerced into a NodeJS stream - I don't think it should be too problematic to do in Node and I'm excited that the streams WG is keeping a prototype for interop. Pinging @mcollina who attended the meeting.
The text was updated successfully, but these errors were encountered: