diff --git a/packages/duoyun-ui/src/elements/modal.ts b/packages/duoyun-ui/src/elements/modal.ts index 834700ba..9bb232f3 100644 --- a/packages/duoyun-ui/src/elements/modal.ts +++ b/packages/duoyun-ui/src/elements/modal.ts @@ -20,7 +20,7 @@ import { mediaQuery } from '@mantou/gem/helper/mediaquery'; import { theme } from '../lib/theme'; import { locale } from '../lib/locale'; import { hotkeys } from '../lib/hotkeys'; -import { DyPromise } from '../lib/utils'; +import { DyPromise, ignoredPromiseReasonSet } from '../lib/utils'; import { setBodyInert } from '../lib/element'; import { commonAnimationOptions, fadeIn, fadeOut, slideInUp } from '../lib/animations'; @@ -183,7 +183,6 @@ export class DuoyunModalElement extends GemElement { @state closing: boolean; // Cannot be used for dynamic forms - // 错误必须处理,不然会被默认通过 Toast 显示 static open(options: ModalOptions & ModalOpenOptions) { const modal = new this({ ...options, open: true }); const restoreInert = setBodyInert(modal); @@ -198,6 +197,7 @@ export class DuoyunModalElement extends GemElement { modal.addEventListener('close', async () => { const ele = getBodyEle(); await options.prepareClose?.(ele); + ignoredPromiseReasonSet.add(ele); rej(ele); }); modal.addEventListener('ok', async () => { diff --git a/packages/duoyun-ui/src/helper/error.ts b/packages/duoyun-ui/src/helper/error.ts index 2fd28cd5..581e8c9c 100644 --- a/packages/duoyun-ui/src/helper/error.ts +++ b/packages/duoyun-ui/src/helper/error.ts @@ -1,4 +1,5 @@ import { Toast } from '../elements/toast'; +import { ignoredPromiseReasonSet } from '../lib/utils'; let unloading = false; addEventListener('beforeunload', () => { @@ -21,6 +22,10 @@ function printError(err: Error | ErrorEvent | DOMException) { } function handleRejection({ reason }: PromiseRejectionEvent) { + if (ignoredPromiseReasonSet.has(reason)) { + ignoredPromiseReasonSet.delete(reason); + return; + } if (reason) { const errors = reason.errors || reason; if (Array.isArray(errors)) { diff --git a/packages/duoyun-ui/src/lib/utils.ts b/packages/duoyun-ui/src/lib/utils.ts index bcb1ceba..950b58b0 100644 --- a/packages/duoyun-ui/src/lib/utils.ts +++ b/packages/duoyun-ui/src/lib/utils.ts @@ -297,3 +297,7 @@ export class DyPromise> extends Promise return Object.assign(result, this) as unknown as DyPromise & E; } } + +// 不应该以发生错误的方式来处理拒绝的 Promise +// 缺点:同时有个相同原因的 Promise 需要用错误处理时会被忽略 +export const ignoredPromiseReasonSet = new WeakSet();