Files
feeddeck/supabase/functions/_shared/utils/fetchWithTimeout.ts
2023-09-03 16:16:38 +02:00

16 lines
478 B
TypeScript

/**
* `fetchWithTimeout` is a wrapper around `fetch` that adds a timeout. If the request takes longer than the timeout, the
* request will be aborted.
*/
export const fetchWithTimeout = async (
url: string,
options: RequestInit,
timeout: number,
) => {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const res = await fetch(url, { ...options, signal: controller.signal });
clearTimeout(id);
return res;
};