为什么获取Body对象只能读取一次? [英] Why fetch Body object can be read only once?
问题描述
fetch
规格声明可读流 Body
包含 Body.bodyUsed
标志,该标志最初设置为 false
然后通过调用任何解析方法设置为 true
。
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.
以下是一个例子:
fetch('/some/path', (res) => {
// res.body.bodyUsed === false
res.json();
// res.body.bodyUsed === true
});
如果您尝试调用类似 res.json()的方法
或 res.text()
再一次抛出异常。
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。在Chrome(可能还有其他浏览器)中,该标记可以作为 res.body.locked
访问。
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?
可以读取 Response.body
不止一次使用 Response.clone()
It is possible to read Response.body
more than once by using Response.clone()
这篇关于为什么获取Body对象只能读取一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!