Skip to content
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

[feature-request]: Async iterable streams on web streams #1245

Open
jimmywarting opened this issue Dec 1, 2022 · 0 comments
Open

[feature-request]: Async iterable streams on web streams #1245

jimmywarting opened this issue Dec 1, 2022 · 0 comments
Labels
library Relates to an Origami library service Relates to an Origami service

Comments

@jimmywarting
Copy link

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
      }
    }
  }
}
@github-actions github-actions bot added the service Relates to an Origami service label Dec 1, 2022
@JakeChampion JakeChampion transferred this issue from polyfillpolyfill/polyfill-service Dec 2, 2022
@github-actions github-actions bot added the library Relates to an Origami library label Dec 2, 2022
@robertboulton robertboulton removed this from Backlog in Origami ✨ Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library Relates to an Origami library service Relates to an Origami service
Projects
None yet
Development

No branches or pull requests

1 participant