All files / client/src/utils fetchWithTimeout.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 2/2
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16            521x         3286x 3286x 3286x    
// Change to AbortSignal.timeout() at some point when browsers mature more.
// A caller-provided signal would be silently overridden by the timeout
// signal, so the type forbids passing one. The timer is deliberately never
// cleared: it must also bound reading the response body (a connection can
// stall mid-body without ever erroring), and aborting an already-settled
// request is a no-op
export const fetchWithTimeout = async (
  url: string,
  timeoutMs: number,
  init?: Omit<RequestInit, "signal">,
): Promise<Response> => {
  const controller = new AbortController();
  setTimeout(() => controller.abort(), timeoutMs);
  return await fetch(url, { ...init, signal: controller.signal });
};