Skip to content

Commit

Permalink
Only use AbortSignal.timeout if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
cleve-fauna committed Aug 21, 2023
1 parent e4eb87a commit 0a13bb0
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/http-client/fetch-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@ export class FetchClient implements HTTPClient {
method,
client_timeout_ms,
}: HTTPRequest): Promise<HTTPResponse> {
const signal =
AbortSignal.timeout === undefined
? (() => {
const controller = new AbortController();
const signal = controller.signal;
setTimeout(() => controller.abort(), client_timeout_ms);
return signal;
})()
: AbortSignal.timeout(client_timeout_ms);

const response = await fetch(this.#url, {
method,
headers: { ...requestHeaders, "Content-Type": "application/json" },
body: JSON.stringify(data),
signal: AbortSignal.timeout(client_timeout_ms),
signal,
keepalive: this.#keepalive,
}).catch((error) => {
throw new NetworkError("The network connection encountered a problem.", {
Expand Down

0 comments on commit 0a13bb0

Please sign in to comment.