Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rejecting a promise throws "Uncaught (in promise)"

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.

like image 794
Javier Marín Avatar asked Apr 24 '18 08:04

Javier Marín


1 Answers

You have to catch it to prevent the error

modal.promise.then(hideFn, hideFn).catch((res) => {});
like image 109
John Velasquez Avatar answered Oct 21 '22 16:10

John Velasquez