The fetch
specification states that the readable stream Body
contains the Body.bodyUsed
flag which is initially set to false
and then is set to true
with a call of any parsing method.
Here's an example:
fetch('/some/path', (res) => {
// res.body.bodyUsed === false
res.json();
// res.body.bodyUsed === true
});
If you try to call a method like res.json()
or res.text()
once again, an exception is thrown.
The question is: why that behavior is used? Why not to allow parsing that readable stream as many times as one wants? I found no explanation of the matter.
PS. In Chrome (and maybe other browsers), that flag is accessible as res.body.locked
.
The question is: why that behavior is used? Why not to allow parsing that readable stream as many times as one wants?
It is possible to read Response.body
more than once by using Response.clone()