HTTP vs Network Errors
404 still fulfills fetch — check response.ok. Network failures reject.
Classic trap: fetch only rejects on network
problems (offline, DNS, CORS block). An HTTP 404/500 still
fulfills with a Response where ok is
false. Throw yourself when !response.ok.
Results appear here.
Important JavaScript
async function getPost(id) {
const response = await fetch(
"https://jsonplaceholder.typicode.com/posts/" + id
);
if (!response.ok) {
throw new Error("HTTP " + response.status);
}
return response.json();
}