* refactor * use abort controller * address comment * fix tests * fix time * fix tests * fix platform library build
14 lines
386 B
TypeScript
14 lines
386 B
TypeScript
export async function fetchWithTimeout(url: string, options: RequestInit = {}, timeoutMs: number) {
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
try {
|
|
const response = await fetch(url, {
|
|
...options,
|
|
signal: controller.signal,
|
|
});
|
|
return response;
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|