Skip to content

Commit c327db8

Browse files
committed
refactor: replace from AbortController to AbortSignal
1 parent cd5aa0e commit c327db8

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

lib/supports/fetch-http-client.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ describe('FetchHttpClient', () => {
107107
await httpClient.request(request);
108108

109109
// then
110-
await expect(doRequest).rejects.toThrowError('Request Timeout: 3000ms');
110+
await expect(doRequest).rejects.toThrowErrorMatchingInlineSnapshot(
111+
'"The operation was aborted due to timeout"',
112+
);
111113
});
112114

113115
test('should use given option if provided', async () => {
@@ -122,6 +124,8 @@ describe('FetchHttpClient', () => {
122124
await httpClient.request(request, httpClientOptions);
123125

124126
// then
125-
await expect(doRequest).rejects.toThrowError('Request Timeout: 1000ms');
127+
await expect(doRequest).rejects.toThrowErrorMatchingInlineSnapshot(
128+
'"The operation was aborted due to timeout"',
129+
);
126130
});
127131
});

lib/supports/fetch-http-client.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@ export class FetchHttpClient implements HttpClient {
1111
request: Request,
1212
options?: HttpClientOptions,
1313
): Promise<Response> {
14-
const controller = new AbortController();
1514
const timeout = options?.timeout ?? this.#timeout;
1615

17-
return await Promise.race<Response>([
18-
fetch(request, { signal: controller.signal }),
19-
new Promise((resolve, reject) =>
20-
setTimeout(() => {
21-
controller.abort();
22-
reject(new Error(`Request Timeout: ${timeout}ms`));
23-
}, timeout),
24-
),
25-
]);
16+
return await fetch(request, { signal: AbortSignal.timeout(timeout) });
2617
}
2718
}

0 commit comments

Comments
 (0)