Deno: Async iteration for fetch response body

Created on 14 Jun 2019  路  4Comments  路  Source: denoland/deno

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.

bug

Most helpful comment

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

All 4 comments

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?

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.

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xueqingxiao picture xueqingxiao  路  3Comments

somombo picture somombo  路  3Comments

motss picture motss  路  3Comments

ry picture ry  路  3Comments

ry picture ry  路  3Comments