Question presented to candidate: "If a fetch() call gets back a 404 response from the server, does the returned Promise reject or resolve? Walk me through exactly how you'd correctly handle that case."
What a strong answer should cover:
- 📌 Interview term:
fetch(url, options)— the standard, Promise-based API for making HTTP requests, returning a Promise that resolves to aResponseobject. - 📌 Interview term: the real, direct answer to the prompt — verified directly against a real local server: a genuine
404response resolves the fetch Promise, not rejects it —response.okis genuinelyfalseandresponse.statusis genuinely404, but no error is thrown and no.catch()runs. A precise answer names that correctly handling this requires an explicit check ofresponse.ok(or the specific status code) after theawait/.then(), not a try/catch alone. - 📌 Interview term: when fetch actually DOES reject — verified directly: only a genuine network-level failure (DNS failure, connection refused, no server listening at all) genuinely rejects the Promise, with a real
TypeError— a real, sharp distinction from an HTTP-level error response. - A precise answer names the real steps to read a response body:
response.json()/response.text()/response.blob()are themselves also Promise-returning methods (reading the body is an async operation), verified directly with a real JSON body parsed correctly. - A precise answer names that
fetchaccepts a secondoptionsobject for method, headers, and body — verified directly with a realPOSTrequest carrying a JSON body and aContent-Typeheader.
Clarifying questions expected:
- None — this is a definitional/technical question; directly answering the prompt's own 404-resolves-not-rejects question is the strong signal, since it is the single most commonly misunderstood detail about fetch.
Code / implementation expected: Yes — a real fetch call against a real 404 endpoint, directly observing that it resolves rather than rejects, is the clearest, most convincing demonstration.