From 01eee8287862c213bfede540d09bcc9f9a478b7b Mon Sep 17 00:00:00 2001 From: Danila Rodichkin Date: Thu, 21 Mar 2024 15:31:48 +0300 Subject: [PATCH] fix: clear abort timeout after response was received (#369) --- src/fetch.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 =