14 lines · 437 B · TypeScript
RawDownload
/** Fetch that aborts when the response headers don't arrive within timeoutMs. */export async function fetchWithTimeout(  url: string,  init: RequestInit,  timeoutMs: number,): Promise<Response> {  const controller = new AbortController();  const timeout = setTimeout(() => controller.abort(), timeoutMs);  try {    return await fetch(url, { ...init, signal: controller.signal });  } finally {    clearTimeout(timeout);  }}