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.
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 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 .
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.
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 .
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.
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.
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.
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With