Rejecting a promise throws "Uncaught (in promise)"
Solution 1
You have to catch
it to prevent the error
modal.promise.then(hideFn, hideFn).catch((res) => {});
Solution 2
just like Amaya said, you must use catch
to catch the error, otherwise it will become a UnhandledPromiseRejection
, different promise
vender have different implementations to handle such kind of UnhandledPromiseRejection
, but in either way, it will just be thrown at some point, so zone.js
will throw the error in the next tick
.
And I see you have already used finally, if you use finally UnhandledPromiseRejection
should not be thrown, but you need to use zone.js 0.8.26
to support Promise.finally
, if the error still exists when you upgrade the zone.js
, please tell me or fire an issue in zone.js
repository, https://github.com/angular/zone.js/issues
Javier Marín
Updated on June 07, 2022Comments
-
Javier Marín almost 2 years
I'm using promises to handle to handle a modal dialog: resolved when the user press the OK button, rejected when cancelled or closed.
To resolve and dismiss the modal I use this methods:
let modalResolve, modalReject; modal.promise = new Promise<any>((resolve, reject) => { modalResolve = resolve; modalReject = reject; }); modal.close = (result) => { if (modal.isOpen) { modalResolve(result); } }; modal.dismiss = (reason) => { if (modal.isOpen) { modalReject(reason); } }; modal.promise.finally(() => modalElement.remove());
And when cancel button fires this method within the modal:
modal.dismiss('close')
Everything is working fine and the modal hides, but a console error is logged with this description and stack:
Error: Uncaught (in promise): close at resolvePromise (zone.js:814) at resolvePromise (zone.js:771) at eval (zone.js:873) at ZoneDelegate.invokeTask (zone.js:421) at Object.onInvokeTask (core.js:4751) at ZoneDelegate.invokeTask (zone.js:420) at Zone.runTask (zone.js:188) at drainMicroTaskQueue (zone.js:595) at ZoneTask.invokeTask [as invoke] (zone.js:500) at invokeTask (zone.js:1540)
It is weird because the modal is dismissed anyway, and this error is not shown on all modals I use, just in some of them. Resolving does not produce this kind error.
-
Javier Marín about 6 yearsIs not .catch(onRejected) supposed to be the same as calling .then(onFulfilled, onRejected) with the second parameter being the .catch() handler?
-
John Velasquez about 6 yearsyes you can do it that way, but base on the error the promise
throws
an exception that is why we have to call the catch to prevent the exception error. -
Javier Marín about 6 yearsThere is not exception thrown, just a call to reject('close')
-
John Velasquez about 6 yearsexample would be this way
var promise1 = new Promise(function(resolve, reject) { throw 'Uh-oh!'; });
that is why the app crashed because of the throw, to prevent it we have to callcatch
-
Javier Marín about 6 yearsDoing
modal.promise.finally(() => { debugger; }).catch(() => { debugger; });
shows the same error after thefinally()
callback but before thecatch()
block -
Javier Marín about 6 yearsThank you, I found that the cause of my error was that I was using
.then()
in other places of my code, without a.catch()
. I thought that with onecatch
for each promise was enough, but it seems it has to be set for each listener -
user3590450 over 4 yearsJavier Marín - Thank you.
-
SUHAIL KC about 4 yearsThank you, Javier Martin. I had the same issue. fixed by using .catch()
-
s4suryapal almost 4 years@JavierMarín please add
.catch()
as a solution,add proper explanation and accept it as answer,we all facing same issue , Thanks