Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

" Uncaught (in promise) " when calling the reject function inside a fetch 'then' method

Here is the code in question:

new Promise((resolve, reject) => {
  const opts = {
    credentials: 'same-origin',
  };

  fetch(`/_api/myAPI`, opts)
  .then((res) => {
    if (!res.ok) {
      reject(res);
    } else {
      ...

If the url throws an exception a 401, when the execution reaches reject(res); it throws Uncaught (in promise)

Even after I add a .catch after the .then call, i.e.

  fetch(`/_api/myAPI`, opts)
  .then((res) => {
    if (!res.ok) {
      reject(res);
    } else {
      ...
   })
  .catch((e) => {
    console.log(e);
   }

it still happens.

Why reject will throw this exception and how can I fix it? My experience is limited to jQuery.Promise and I don't a reject within a failure handler will trigger this error.

like image 602
Anthony Kong Avatar asked Apr 23 '16 03:04

Anthony Kong


People also ask

How do you handle a Promise reject?

We must always add a catch() , otherwise promises will silently fail. In this case, if thePromise is rejected, the execution jumps directly to the catch() method. You can add the catch() method in the middle of two then() methods, but you will not be able to break the chain when something bad happens.

What does uncaught in Promise?

What does that log "Uncaught (in promise)" mean? It means that there was an error in one of our promises, but we did not write any code in order to handle that error and try to catch it. The way you catch an error in a promise is you chain a .

What happens if multiple resolve or reject is called within a Promise?

The only thing to understand is that once resolved (or rejected), that is it for a defered object - it is done. If you call then(...) on its promise again, you immediately get the (first) resolved/rejected result. Additional calls to resolve() will not have any effect.

What happens when you reject a Promise Javascript?

The static Promise. reject function returns a Promise that is rejected. For debugging purposes and selective error catching, it is useful to make reason an instanceof Error .

What is the use of the promise reject method?

The Promise.reject() method returns a Promise object that is rejected with a given reason. The source for this interactive example is stored in a GitHub repository.

How does JavaScript handle uncaught errors in promise chain?

Within a promise chain this error can be caught down the line with a catch method callback, but if none is there, the JavaScript engine will deal with the error like with any other uncaught error, and apply the default handler in such circumstances, which results in the output you see in the console.

What is uncaught in a promise?

Uncaught (in promise) Unauthorized access to the user data If the promise is resolved, you can omit the catch () method. In the future, a potential error may cause the program to stop unexpectedly.

What is the use of then() method in promise in JavaScript?

The .then () method should be called on the promise object to handle a result (resolve) or an error (reject). It accepts two functions as parameters. Usually, the .then () method should be called from the consumer function where you would like to know the outcome of a promise's execution.


1 Answers

When you're rejecting the promise, you're immediately rejecting the promise that is wrapping that entire operation, so you would never get to that catch block.

An analogy: reject and resolve are to promises as return is to functions.

I think what you are trying to do is the code below.

new Promise((resolve, reject) => {
  const opts = {
    credentials: 'same-origin',
  };
  fetch(`/_api/myAPI`, opts)
  .then((res) => {
    if (!res.ok) {
      return Promise.reject()
    } else {
      ...
      resolve(...);
   })
  .catch((e) => {
    console.log(e);
    reject();
   }
}
like image 179
gnicholas Avatar answered Oct 17 '22 08:10

gnicholas