Streaming fetch Responses
Process large responses as they arrive.
Responses Are Streams
A fetch Response exposes its body as a ReadableStream via response.body. Instead of awaiting the full payload, you can process bytes as they arrive.
const res = await fetch("/big-file");
const stream = res.body; // ReadableStream of Uint8ArrayReading the Body Stream
response.body yields Uint8Array chunks. Get a reader and loop just like any readable stream.
const reader = res.body.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) break;
console.log("received", value.length, "bytes");
}All lessons in this course
- Readable Streams
- Streaming fetch Responses
- Transform Streams
- Backpressure and Piping