diff --git a/src/fetch.ts b/src/fetch.ts index c0021c0..e2cb5d2 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -149,10 +149,15 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { } } + let abortTimeout: NodeJS.Timeout | undefined; + // TODO: Can we merge signals? if (!context.options.signal && context.options.timeout) { const controller = new AbortController(); - setTimeout(() => controller.abort(), context.options.timeout); + abortTimeout = setTimeout( + () => controller.abort(), + context.options.timeout + ); context.options.signal = controller.signal; } @@ -167,6 +172,10 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { await context.options.onRequestError(context as any); } return await onError(context); + } finally { + if (abortTimeout) { + clearTimeout(abortTimeout); + } } const hasBody =