- From: Mattias Buelens <notifications@github.com>
- Date: Tue, 16 Jan 2024 02:02:52 -0800
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
Received on Tuesday, 16 January 2024 10:02:59 UTC
@liudonghua123 You shouldn't use `getReader()` or `reader.read()` in this case, instead you should `for await` over the `ReadableStream` itself:
```javascript
const readable = response.body;
if (!readable) {
throw new Error('Failed to get response body');
}
let receivedLength = 0;
let chunks = [];
for await (const value of readable) {
chunks.push(value);
receivedLength += value.length;
console.log(`Received ${receivedLength} of ${contentLength}`)
}
```
[MDN has more examples on async-iterating a stream.](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#async_iteration_of_a_stream_using_for_await...of) 😉
--
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/778#issuecomment-1893421404
You are receiving this because you are subscribed to this thread.
Message ID: <whatwg/streams/issues/778/1893421404@github.com>
Received on Tuesday, 16 January 2024 10:02:59 UTC