[feature-request]: Async iterable streams on web streams
jimmywarting opened this issue · 0 comments
jimmywarting commented
What
Working with streams on either web streams or node streams are troublesome. cuz they are inconsistent with each other.
Therefore ppl resort to a more lower level syntax that works for both node:stream
and web streams with the help of async for loop and Symbol.asyncIterators
for await (const item of readable) {
console.log(item)
}
Making another transformation to the stream is easy too. just need to do:
/**
* @param {ReadableStream | node.Readable} iterable
*/
async function * toUpperCase(iterable) {
for await (const item of readable) yield item.toUppercase()
}
for await (const item of toUpperCase(readable)) {
console.log(item)
}
NodeJS, deno, and web streams polyfill and the spec have this Symbol.asyncIterator
but non of the browser have this and there are a few tickets on this on their part.
I just wish for there to be a polyfill / patch in this missing feature, so that we can start using it. (not neccessary have to include the hole stream polyfill.
if (typeof ReadableStream !== 'undefined' && !ReadableStream.prototype[Symbol.asyncIterator]) {
ReadableStream.prototype[Symbol.asyncIterator] = function () {
const reader = this.getReader()
let last = reader.read()
return {
next () {
const temp = last
last = reader.read()
return temp
},
return () {
return reader.releaseLock()
},
throw (err) {
this.return()
throw err
},
[Symbol.asyncIterator] () {
return this
}
}
}
}