Skip to content

QuizNot attempted

Spot the bug: error handling for two fetches

Which statements about this error handling are true?
try {
const [resourcesRes, rawRes] = await Promise.all([
fetch('/api/resources'),
fetch('/api/raw'),
]);
setResourceType((await resourcesRes.json()).map(toOption));
setResourceData(await rawRes.json());
} catch (error) {
console.log('Error:', error);
}

Select all that apply.

1–4 to choose, Enter to submit
Show answer & explanation

Correct answers:

  • A 404 whose body is a JSON error object is treated as success and stored as table rows
  • On failure the user just sees an empty table: no error message and no retry
  • If either request fails, the result of the other one is lost too
  • fetch only rejects on network failure, CORS or abort
  • a 404 or 500 resolves normally.
  • Check if (!res.ok) throw new Error("HTTP " + res.status) before .json(), keep the error in state and render a message with a retry.
  • Promise.all rejects on the first failure, so use Promise.allSettled (or two independent requests) when one half is useful alone.
  • There is also no loading state.
  • Not fetch rejects on non-2xx: it resolves with ok: false
  • only parsing an HTML error page would throw, and then only by accident.