import {TimeoutError} from '../errors/TimeoutError.js'; export type TimeoutOptions = { timeout: number; fetch: typeof fetch; }; // `Promise.race()` workaround (#91) export default async function timeout( request: Request, init: RequestInit, abortController: AbortController | undefined, options: TimeoutOptions, ): Promise { return new Promise((resolve, reject) => { const timeoutId = setTimeout(() => { if (abortController) { abortController.abort(); } reject(new TimeoutError(request)); }, options.timeout); void options .fetch(request, init) .then(resolve) .catch(reject) .then(() => { clearTimeout(timeoutId); }); }); }