Ive seen many eexamples in the internet where the fetch api calls are like
async function getData() {
const url = "https://example.org/products.json";
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(Response status: ${response.status});
}
return json
const json = await response.json();
} catch (error) {
return error;
}
}
but why are we throwing errors just to return it? cant we directly return error when !reponse.ok
.
here is wht i mean
async function getData() {
const url = "https://example.org/products.json";
const response = await fetch(url);
if (!response.ok) {
return new Error(Response status: ${response.status});
}
const json = await response.json();
return json
}