Why fetch Body object can be read only once?

為{幸葍}努か 提交于 2019-12-12 16:27:08

问题


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.


回答1:


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()



来源:https://stackoverflow.com/questions/46742251/why-fetch-body-object-can-be-read-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!