response.body is a ReadableStream of Uint8Array chunks, there as soon as fetch() fulfills, before the body has arrived. res.json() and res.text() wait for all of it; reading the stream yourself lets you handle bytes as they land.
Call res.body.getReader() and loop on await reader.read(), which resolves to { value, done }. Or iterate with for await (const chunk of res.body), supported since Chrome 124, Firefox 110 and Safari 27. Either way the stream is now locked: res.json() rejects with a TypeError. Breaking out of for await cancels the stream, as does reader.cancel(); aborting the fetch's signal errors it.
Text. Chunks can split anywhere, including inside a multi-byte UTF-8 character. Pipe through new TextDecoderStream(), or reuse one TextDecoder with decode(chunk, { stream: true }). Decoding each chunk separately turns a split é into two �. For line-based formats such as NDJSON, also buffer the unfinished last line.
Progress. Add up chunk.byteLength and divide by Content-Length. Two catches:
- With chunked transfer encoding there is no
Content-Length, so show indeterminate progress. - With
Content-Encoding: gziporbr, the header is the compressed size while chunks arrive decompressed. A 100,000-byte body sent gzipped had aContent-Lengthof 132. Have the server send the uncompressed size in its own header, or show bytes rather than percent.
Uploads. A ReadableStream request body needs duplex: 'half', and only Chrome supports it (105 and later); in Chrome 156 it also needed HTTP/2. Upload progress is the one thing XMLHttpRequest's upload events still give you that fetch() doesn't, portably.