0

I have the following code:

fetch(
  url,
  { ...data }
).then((response) => {
  if (!response.ok) throw new Error(response.statusText);
  return response.json();
}).then((response) => {
  resolve(response);
}).catch((error) => {
  console.log('error', error);
  reject(error);
});

When I perform a request and get a 404, the console.log('error') line runs, but I still get an error int he console:

GET https://swapi.co/api/people/0/ 404 ()

Uncaught (in promise) Error
    at http.js:10

I can't figure out why this is happening, if the catch() block is running, why does it say uncaught (in promise)?

imtheman
  • 4,713
  • 1
  • 30
  • 30
Abdul Ahmad
  • 9,673
  • 16
  • 64
  • 127

1 Answers1

1

You call reject, so the promise this points to will get rejected, and if you don't catch that that is uncaught. to resolve that see:

What is the explicit promise construction antipattern and how do I avoid it?

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151