取回请求返回的中间结果应该被称为什么?斑点还是只是回应? [英] What should the intermediate result that a fetch request returns be called? A blob or just response?
问题描述
当我们在JS中使用 fetch
发出get请求时,我们通常会这样做
when we use fetch
in JS to issue a get request, we normally do thing like this
fetch(endpoint).then(res => res.json()).then(result => ...)
但是,我正在观看Wes Bos的JS 30课程,他称中间结果是,获取结果返回 blob
,如
However I was watching Wes Bos's JS 30 courses and he called the intermediate result that the fetch returns a blob
as in
fetch(endpoint).then(blob => blob.json()).then(result => ...)
我在 https://developer处找到了blob的定义.mozilla.org/zh-CN/docs/Web/API/Blob
我不足以判断Wes Bos在这里是否使用正确的术语将其称为 blob
,而且我无法直接与他联系并询问他.希望我能在这里找到一些答案.
I am not knowledgable enough to judge if Wes Bos was using the right term here to refer to it as blob
and I have no ways to contact him directly and ask him. Hope I can find some answers here.
推荐答案
fetch
返回 Response
对象,而不是 Blob
-如果您尝试使用像 .slice
和 .stream
结果,将抛出错误,因为这些方法不存在.
fetch
returns a Response
object, not a Blob
- if you try to use blob methods like .slice
and .stream
on the result, errors will be thrown, since those methods do not exist.
// Not OK:
fetch('data:,Hello%2C%20World!').then(blob => blob.slice()).catch((err) => console.log('err', err.message));
// OK:
fetch('data:,Hello%2C%20World!').then(res => res.text()).then(console.log);
请注意,响应可以转换为Blob,但 fetch
的返回值仍然是响应:
Note that the Response can be converted into a Blob, but the return value from fetch
would still be a Response:
fetch(endpoint)
.then(response => response.blob())
.then((blob) => {
// work with the blob here
});
将响应称为Blob是不正确的.它们有些相似,但不相同.最好避免将其称为
Calling the response a blob is incorrect. They're somewhat similar, but not the same. Better to avoid calling it a blob
to avoid confusion.
这篇关于取回请求返回的中间结果应该被称为什么?斑点还是只是回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!