Which branch runs when the server returns 404?
fetch('/missing')
.then((r) => {
if (!r.ok) return 'bad';
return 'good';
})
.catch(() => 'caught')
.then((v) => console.log(v));
- good
- bad
- caught
- nothing, promise rejects silently
VaultBoy
bad.
404 doesn’t reject fetch(), so the first then runs, r.ok is false, and you end up logging 'bad'. catch() only kicks in for network failures, not HTTP status codes.
404 is one of those “computer says yes” situations: fetch() resolves, your first then runs, r.ok is false, and you log "bad".
catch() only fires on an actual rejection (network failure etc) unless you deliberately throw when !r.ok, which is what people usually mean by “treat 404 as an error”.