mirror of
https://github.com/JetSprow/J-Board-Lite.git
synced 2026-05-01 09:14:11 +05:30
30 lines
697 B
TypeScript
30 lines
697 B
TypeScript
export class TimeoutError extends Error {
|
|
constructor(message: string) {
|
|
super(message);
|
|
this.name = "TimeoutError";
|
|
}
|
|
}
|
|
|
|
export async function fetchWithTimeout(
|
|
input: RequestInfo | URL,
|
|
init: RequestInit = {},
|
|
timeoutMs = 10_000,
|
|
): Promise<Response> {
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
|
|
try {
|
|
return await fetch(input, {
|
|
...init,
|
|
signal: init.signal ?? controller.signal,
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof Error && error.name === "AbortError") {
|
|
throw new TimeoutError("请求超时");
|
|
}
|
|
throw error;
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|