When your TypeScript code fetches a large file or text response, response.json() and response.text() wait until the full body has arrived before giving your code a parsed value or text. To process data as it arrives, read response.body, the Fetch API stream that carries the response bytes. A ReadableStream delivers the bytes in chunks, or pieces of data. Your code can handle each piece and move on without first collecting the full response.
Streaming helps your code avoid keeping the complete response in memory if the chunk handler does not save every chunk. Your code still uses growing amounts of memory if it adds each chunk to one string or array. For smaller responses, or when you need the complete parsed result or file, the ordinary body methods are simpler.
Decode and process text as it arrives
A Fetch response body contains bytes, so your code must decode them to handle text. The browser’s TextDecoderStream converts the byte stream into text chunks. It also handles a character whose bytes are split across two chunks.
async function consumeText(
url: string,
processChunk: (text: string) => void,
): Promise<void> {
const response = await fetch(url);
if (response.body === null) {
throw new Error("The response has no body to read.");
}
const textStream = response.body.pipeThrough(new TextDecoderStream());
for await (const chunk of textStream) {
processChunk(chunk);
}
// This point runs after the stream has been consumed.
}
processChunk should handle each fragment and let it go when it no longer needs it. For example, it can update a running count or pass a line to another part of your code. If it adds every fragment to one string, your code still keeps the complete response.
A chunk is the piece of data a stream provides in one read. The stream does not guarantee that a chunk’s size or boundaries match a line, record, or character. The Streams Standard describes chunks as units chosen by the stream, and the Fetch API exposes response bodies as readable streams. If your code needs complete lines or records, it must keep any unfinished fragment from one chunk and join it with the next.
The for await loop waits for each chunk before it continues. The loop ends when the stream is complete, so code after it runs only after your code has consumed the stream. Streams also support backpressure, which lets a consumer signal the source to slow down when it is not ready for more data.
Read bytes directly when you need them
For binary data or custom decoding, read the response body directly with a reader. Each read() call returns a result with done and, while data remains, value. When done: true, the stream has closed. Otherwise, value holds the next chunk of bytes. MDN shows this pattern in its readable stream examples.
async function consumeBytes(
url: string,
processChunk: (bytes: Uint8Array) => void,
): Promise<void> {
const response = await fetch(url);
if (response.body === null) {
throw new Error("The response has no body to read.");
}
const reader = response.body.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
processChunk(value);
}
} finally {
reader.releaseLock();
}
}
Calling getReader() locks the stream to that reader. Another reader cannot consume it until the code releases the lock. The finally block releases the lock even if reading or chunk processing fails. Choose streaming or a whole-body method for a response body. Once your code reads or locks the body, it cannot consume that same body again.
Use whole-body methods when you need the whole result
Use response.json() when your code needs a parsed JavaScript value, such as an object or array. Its promise resolves after the response body has been read and parsed as JSON. Use response.blob() when your code needs the complete body as a Blob, such as to create an image object URL. These methods work well when your code needs the whole result, but they do not give it chunks to process as they arrive. See MDN on json() and blob().
Choose based on what your code needs to do. Stream a large text or binary response when your code can process and discard pieces as they arrive. Use a whole-body method when it needs the complete parsed value or file. Streaming does not set a memory limit by itself. If you want to keep memory use down, your chunk handler must avoid accumulating the response.