What Should The Intermediate Result That A Fetch Request Returns Be Called? A Blob Or Just Response?
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 => ...) However I was watching Wes Bos's
Solution 1:
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);
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
});
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.
Post a Comment for "What Should The Intermediate Result That A Fetch Request Returns Be Called? A Blob Or Just Response?"