I'm new to coding and was trying to implement download progress.Below is my code.
let btn = document.querySelector("#img");
btn.addEventListener("click", loadimage);
function loadimage() {
fetch("https://reqres.in/invalid-url")
.then(async (res) => {
let contLength = res.headers.get("content-length");
let reader = res.body.getReader();
let downloaded_data = 0;
while (true) {
const { done, value } = await reader.read();
if (done) {
console.log("completed");
break;
}
downloaded_data += value.length;
console.log(downloaded_data);
}
})
.catch(function (err) {
console.log("catch block");
console.log(err);
});
}`
` This is the error I'm getting.(not getting caught by catch block)
`GET https://reqres.in/invalid-url 404`
`6939`
`completed`
Why inspite of error, function in then block is getting invoked. I have tried different APIs but all are resulting same