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:
JavaScript
x
17
17
1
let modalResolve, modalReject;
2
modal.promise = new Promise<any>((resolve, reject) => {
3
modalResolve = resolve;
4
modalReject = reject;
5
});
6
modal.close = (result) => {
7
if (modal.isOpen) {
8
modalResolve(result);
9
}
10
};
11
modal.dismiss = (reason) => {
12
if (modal.isOpen) {
13
modalReject(reason);
14
}
15
};
16
modal.promise.finally(() => modalElement.remove());
17
And when cancel button fires this method within the modal:
JavaScript
1
2
1
modal.dismiss('close')
2
Everything is working fine and the modal hides, but a console error is logged with this description and stack:
JavaScript
1
12
12
1
Error: Uncaught (in promise): close
2
at resolvePromise (zone.js:814)
3
at resolvePromise (zone.js:771)
4
at eval (zone.js:873)
5
at ZoneDelegate.invokeTask (zone.js:421)
6
at Object.onInvokeTask (core.js:4751)
7
at ZoneDelegate.invokeTask (zone.js:420)
8
at Zone.runTask (zone.js:188)
9
at drainMicroTaskQueue (zone.js:595)
10
at ZoneTask.invokeTask [as invoke] (zone.js:500)
11
at invokeTask (zone.js:1540)
12
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.
Advertisement
Answer
You have to catch
it to prevent the error
JavaScript
1
2
1
modal.promise.then(hideFn, hideFn).catch((res) => {});
2