front/src/utils/withTimeout.ts
zzh bd97ed76fd feat: ?????????????????/???????
??????????????????????????????? H5/?????

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-30 15:16:55 +08:00

25 lines
630 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** Promise 超时包装:超时后 reject,不取消原任务。 */
export function withTimeout<T>(
promise: Promise<T>,
ms: number,
message = 'TIMEOUT',
): Promise<T> {
return new Promise<T>((resolve, reject) => {
const timer = window.setTimeout(() => reject(new Error(message)), ms);
promise.then(
(value) => {
window.clearTimeout(timer);
resolve(value);
},
(err) => {
window.clearTimeout(timer);
reject(err);
},
);
});
}
export function isTimeoutError(error: unknown): boolean {
return error instanceof Error && error.message === 'TIMEOUT';
}