Skip to content

Commit

Permalink
fix: clear abort timeout after response was received (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniluk4000 authored Mar 21, 2024
1 parent 28a2650 commit 01eee82
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 =
Expand Down

0 comments on commit 01eee82

Please sign in to comment.