Files
feeddeck/supabase/functions/_shared/utils/fetchWithTimeout.ts
Rico Berger 37cd4dff6f [core] Run "deno fmt" (#65)
Format the Deno code via "deno fmt" to use the defined code style from
the "deno.json" file.
2023-11-04 15:45:42 +01: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;
};