I understand that a response of a fetch(), if there is one, will arrive asynchronously as some kind of a “promise” Object, but what is a “BLOB” of such a response Object.
The first time I learned about the term BLOB ("Binary Large OBject) was about BIOS or firmware.
Why should we talk about BLOBs in the context of vanilla JavaScript?
Blobs would be good in generating images in the browser or performing client-side image manipulation, such as resizing, cropping, applying filters or transformations, and so on.
There is no predefined “first in the blob there is 16 bytes of data that identify its type, and then there’s…”
Like in an HTML page, first there is an HTML tag. It has a specified format. Blobs dont have that. They are unspecified data; it’s just “whatever the fetch got, regurgitate it. Maybe its text, maybe its binary gibberish, but spit it out”.
What he means is that when you call fetch there are zero runtime guarantees of what you get back. Depending on the URL it might be Javascript, CSS, an exe file, a zip file, a movie,…
The lowest common denominator of all of these is that they are blobs of data. Hence blob.
Coming across this thread help me out in a game that I’m developing as I’m doing what is called lazy preloading and I thought to myself hey this could be useful with using blob.
// Function to fetch and load image
const fetchAndLoadImage = async (imagePath) => {
try {
let response = await fetch(imagePath);
let blob = await response.blob();
let img = new Image();
img.src = URL.createObjectURL(blob);
return img;
} catch (error) {
console.error(error);
}
};
// parse the response as JSON
const data = await response.json();
id = data.first_id;
let imagePath = baseUrl + data.image;
imageObj = await fetchAndLoadImage(imagePath);
await updateImageSource(imageObj);
await fetchWord(); // fetch the answers
await fetchQuestion();
Quite off-topic, but it’s either preloading (loading something before you need it) or lazy loading (deferring the loading of something until you really need it). Lazy preloading is an oxymoron