Response Body Consumes Once bodyUsed And clone
response.json() and response.text() drain the same stream once; bodyUsed flips to true and a second read throws, while clone forks the body for two consumers.
Code
// response.json() and response.text() are one-shot: a consumed body cannot be read again.
const response = await fetch("https://httpbingo.org/json");
const body = await response.json();
console.log("first read:", body.slideshow.title);
// bodyUsed flips to true after any consume call
console.log("bodyUsed:", response.bodyUsed);
// reading again throws TypeError instead of returning empty data
try {
await response.json();
} catch (error) {
console.log("second read:", (error as TypeError).name);
}
// text() reads as a string; json() parses — same underlying stream
const response2 = await fetch("https://httpbingo.org/json");
const raw = await response2.text();
console.log("text length:", raw.length, "bodyUsed:", response2.bodyUsed);
// clone() forks the body so both consumers can read once each
const response3 = await fetch("https://httpbingo.org/json");
const twin = response3.clone();
const [asText, asJson] = await Promise.all([response3.text(), twin.json()]);
console.log("clone paths:", asText.length > 0, typeof asJson);What the code does
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
Everything operates on top-level bindings: response, body, response2, raw.
What to watch for
- The snippet leans on
fetch— browser-only surfaces a bare Node process cannot provide.
Applying it
The demo output is labeled (first read:, bodyUsed:, second read:), so every printed line ties back to the operation behind it.
Statements that matter
const [asText, asJson] = await Promise.all([response3.text(), twin.json()]);
const response2 = await fetch("https://httpbingo.org/json");
const response3 = await fetch("https://httpbingo.org/json");
Interactive Tools for This Topic
FAQ
- Are there any imports to resolve outside the language?
- No. Every import resolves against the language's own modules. Its state lives in module-level bindings: `response`, `body`, `response2`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.