From Introducing asynchronous JavaScript and Graceful asynchronous programming with Promises.
If I change coffee.jpg to say cof.jpg catch doesn’t show that cof.jpg is doesn’t exist
from on of your examples with the image name changed
From Introducing asynchronous JavaScript and Graceful asynchronous programming with Promises.
If I change coffee.jpg to say cof.jpg catch doesn’t show that cof.jpg is doesn’t exist
from on of your examples with the image name changed
Sorry I think I understand.
catch only works if there’s a network error.
If I put this in the fetch it will work
if (!response.ok) {
throw Error(response.statusText);
so code as follows
fetch(‘https://raw.githubusercontent.com/mdn/learning-area/master/javascript/asynchronous/promises/cof.jpg’).then((response) => {
if (!response.ok) {
throw Error(response.statusText);
}
console.log('It worked :)')
return response.blob();
}).then((myBlob) => {
let objectURL = URL.createObjectURL(myBlob);
image = document.createElement(‘img’);
image.src = objectURL;
document.body.appendChild(image);
}).catch((error) => {
console.log(error);
});
console.log (‘All done!’);