Body Used and ReadableStream
What you'll learn: Why you can only read a fetch response body once, and how the bodyUsed flag protects you from errors.
When you call methods like .json() or .text() on a Response or Request object, you're consuming a stream of data. Think of it like drinking from a bottle — once you've drunk the contents, the bottle is empty. You can't drink from it again unless you get a new bottle (or clone it first).
The bodyUsed Flag
The bodyUsed property is a boolean that tracks whether the body has already been read. It starts as false and becomes true after you consume the body with any method (.json(), .text(), .blob(), etc.).
const response = await fetch('https://api.example.com/data');
console.log(response.bodyUsed); // false
const data = await response.json();
console.log(response.bodyUsed); // true
// This will throw an error!
// const again = await response.json();
Why This Matters
Under the hood, response bodies are ReadableStreams — they flow data in chunks rather than loading everything into memory at once. This is memory-efficient, but it means the data passes through only once. After consumption, the stream is closed.
Using Clone to Read Twice
If you genuinely need to read the same body multiple times, clone the response first:
const response = await fetch('https://api.example.com/data');
const clonedResponse = response.clone();
const data1 = await response.json();
const data2 = await clonedResponse.json(); // Works!
Key Takeaway: Response bodies can only be read once because they're streams. Always check bodyUsed or clone the response if you need multiple reads.