sr✳SHUBHAM RAJFRONTEND ENGINEER
FULL TECHNICAL GUIDEIntermediate3 min read

Why does fetch not reject for HTTP 404 or 500?

The fetch Promise typically fulfills when an HTTP response arrives, even if the server returned an error status.

JavaScript#fetch#http
THE ANSWER / PLAIN ENGLISH

The idea to remember.

The fetch Promise typically fulfills when an HTTP response arrives, even if the server returned an error status. Check response.ok or response.status and explicitly handle error bodies.

From first example to real project.

Start with the smallest working idea, examine a more detailed example, then look at a real application pattern. Adapt dependencies, error handling and data models to your project.

Basic: promise resolution

01 / BEGINNER

Promise callbacks run after synchronous code.

JAVASCRIPT / EXAMPLE
console.log('A');
Promise.resolve().then(() => console.log('C'));
console.log('B'); // A, B, C

Intermediate: await and errors

02 / INTERMEDIATE

Handle unsuccessful HTTP responses as well as rejected requests.

JAVASCRIPT / EXAMPLE
async function loadUser(id) {
  const response = await fetch('/api/users/' + id);
  if (!response.ok) throw new Error('Unable to load user');
  return response.json();
}

Real scenario: parallel independent calls

03 / REAL SCENARIO

Promise.all starts independent calls together and handles failures.

JAVASCRIPT / EXAMPLE
async function loadDashboard() {
  const [account, activity] = await Promise.all([
    fetch('/api/account').then(r => { if (!r.ok) throw Error('Account'); return r.json(); }),
    fetch('/api/activity').then(r => { if (!r.ok) throw Error('Activity'); return r.json(); })
  ]);
  return { account, activity };
}

Try it in your own words.

Explain why does fetch not reject for HTTP 404 or 500 without looking at the code. Then modify the intermediate example, describe one trade-off and identify when the real-world pattern fits.

Keep learning here.

Explore more in-depth guides, exercises and related interview questions in this library.

Browse JavaScript study guides ↗
← Back to question library

Have something
in mind?

Start a conversation