Abdul Ahmad
Abdul Ahmad

Reputation: 10021

getting error in console when using fetch.catch

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)?

Upvotes: 0

Views: 726

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138234

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?

Upvotes: 1

Related Questions