Skip to the solution
MediumFrontend6 min read

How does the fetch API work?

1.1k views

The problem

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 a Response object.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly against a real local server: a genuine 404 response resolves the fetch Promise, not rejects it β€” response.ok is genuinely false and response.status is genuinely 404, but no error is thrown and no .catch() runs. A precise answer names that correctly handling this requires an explicit check of response.ok (or the specific status code) after the await/.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 fetch accepts a second options object for method, headers, and body β€” verified directly with a real POST request carrying a JSON body and a Content-Type header.

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.

  • fetch

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof, against a real live server: a 404 resolves the fetch Promise (check response.ok), only a network failure genuinely rejects

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 49 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

When should you use Map and Set over objects and arrays?

Continue