Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Converting to ReadableStream #157

Closed
jimmywarting opened this issue Sep 27, 2021 · 4 comments
Closed

Converting to ReadableStream #157

jimmywarting opened this issue Sep 27, 2021 · 4 comments

Comments

@jimmywarting
Copy link

jimmywarting commented Sep 27, 2021

I have heard the proposal of ReadableStream.from(iterable)

But there is also things like blob.stream() that returns a RedableStream

So my idea where just to have a similar method on the prototype to do kind of the same thing as Blob.stream() in a way that you can do:

function* naturals() {
  let i = 0
  while (true) {
    yield i
    i += 1
  }
}

const reader = naturals()
  .take(5)
  .stream() // => ReadableStream<numbers>
  .getReader()

This could work for both sync and async iterators.


Note this is just a alternative proposal if we don't get ReadableStream.from(iterable)
or we could have both, but it would seem unnecessary?

@ljharb
Copy link
Member

ljharb commented Sep 27, 2021

Streams aren’t part of the language, and node and browsers have different stream approaches. I’m not sure how this proposal could (or should) specify either?

@bakkot
Copy link
Collaborator

bakkot commented Oct 22, 2021

See previous discussion of other toWhatever methods in #36 (comment), #17, #125. tl;dr is that I think toArray is the only one which makes sense right now.

Regarding the concrete suggestion here:

Note this is just a alternative proposal if we don't get ReadableStream.from(iterable) or we could have both, but it would seem unnecessary?

Note that this proposal is for iterator helpers, not iterable helpers. So even if there were a .stream() method in this proposal, you'd still need to get an iterator out of your iterable first, and so I think ReadableStream.from(iterable) would still be justified. (But because in JS iterators are normally also iterable, you will be able to do ReadableStream.from(iterator) as well.)

Anyway, ReadableStream.from is very likely to happen, so I don't think we should be thinking too hard about alternatives to it in case it doesn't.

@benjamingr
Copy link
Contributor

Note that Node.js intends to implement this proposal on its streams (so you won't have to go through iterators every time + it lets us support concurrency and cancellation. You will be able to use the Node.js helpers to convert an iterable to a web readable stream.

@codehag
Copy link
Collaborator

codehag commented Dec 2, 2021

I will close this a there doesn't appear to be something actionable, if there are other adjustments to readable streams, it might be a good idea to look at the whatwg repos. Thanks for raising it!

@codehag codehag closed this as completed Dec 2, 2021
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

5 participants