Response: bodyUsed property
The bodyUsed property indicates whether the response body has already been read.
Value
A boolean.
true: the body has been read or canceled. After that, all four reading methods reject:TypeError: Body has already been read.false: not read yet. Only getting a reader onbodywithout reading doesn't count as read.- Read-only; assigning to it throws
TypeError. - It becomes
truewhen the content is read and never goes back tofalse. It has no change event: reading is started by your own code. - For a response without a body (
bodyisnull), it staysfalseafter reading, and you can read repeatedly.
Examples
js
async function main() {
const res = await fetch('https://httpbin.org/get');
await res.json();
console.log(res.bodyUsed);
try {
await res.json();
} catch (err) {
console.log(err instanceof Error ? err.message : err);
}
}
main();Result: It prints true and Body has already been read.
Differences from the web
Same as on the web.