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

Async iteration for fetch response body #2520

Closed
MarkTiedemann opened this issue Jun 14, 2019 · 4 comments · Fixed by #2563
Closed

Async iteration for fetch response body #2520

MarkTiedemann opened this issue Jun 14, 2019 · 4 comments · Fixed by #2563
Labels
bug Something isn't working correctly

Comments

@MarkTiedemann
Copy link
Contributor

let response = await fetch(url);
for await (let chunk of response.body) {
  // ...
}

Currently failing with error TS2504: Type 'Body' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.

@ry ry added the bug Something isn't working correctly label Jun 14, 2019
@ry ry mentioned this issue Jun 14, 2019
43 tasks
@MarkTiedemann
Copy link
Contributor Author

MarkTiedemann commented Jun 14, 2019

This feature would require implementing parts of the Streams API, for example, ReadableStream and related classes.

@ry What are your thoughts on the Streams API? Should it be supported in Deno eventually?

@ry
Copy link
Member

ry commented Jun 14, 2019

fetch should be compatible with web APIs - so we should do it. It should be straightforward - all the infrastructure is in place to do this. I believe ResponseBody is already a "Writer", for example.

What are your thoughts on the Streams API?

I think it's poorly designed - which is why we have Reader and Writer in Deno.

@MarkTiedemann
Copy link
Contributor Author

I think it's poorly designed

I agree that it's more complicated than Deno's API, but is it really so bad that we don't ever want to support it? I think for this use-case, for example, it's actually fairly similar.

// Deno API
let { eof, nread } = await response.body.read(buffer)

// Streams API
let reader = response.body.getReader({ mode: 'byob' })
let { done, value } = await reader.read(buffer)

Personally, I'd like Deno to be compatible with the Streams API to be even more browser-friendly. On one hand, the Streams API is still fairly experimental so it might be best to wait. On the other hand, I can also imagine that, whenever the Streams API is stable, we have 3 major competing JS stream APIs (Web, Node, Deno) further fragmenting the JS ecosystem.

@ry
Copy link
Member

ry commented Jun 14, 2019

We must support it to be browser compatible . See toAsyncIterator() for an example of how to convert between Reader and ReadableStream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants