Skip to content

Commit

Permalink
fix: internetReachability aborts handle cancel correctly (#700)
Browse files Browse the repository at this point in the history
InternetReachability aborts pending request by calling abort() of AbortController. According to AbortController API when abort() is called, the fetch() promise rejects with an Error of type DOMException, with name AbortError. So reject('canceled') will be ignored and checking error !== 'canceled' is useless.

The right way is to use abort('canceled') to set reject reason to 'canceled' instead of DOMException.

Previously because of that InternetReachability aborts previous request and calls _setIsInternetReachable(false).
  • Loading branch information
gizomo committed Nov 29, 2023
1 parent 86bf605 commit 0a36296
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/internal/internetReachability.ts
Expand Up @@ -79,26 +79,19 @@ export default class InternetReachability {

// Create promise that will reject after the request timeout has been reached
let timeoutHandle: ReturnType<typeof setTimeout>;
const timeoutPromise = new Promise<Response>(
(_, reject): void => {
timeoutHandle = setTimeout((): void => {
controller.abort();
reject('timedout');
}, this._configuration.reachabilityRequestTimeout);
},
);
const timeoutPromise = new Promise<Response>((): void => {
timeoutHandle = setTimeout(
(): void => controller.abort('timedout'),
this._configuration.reachabilityRequestTimeout,
);
});

// Create promise that makes it possible to cancel a pending request through a reject
// eslint-disable-next-line @typescript-eslint/no-empty-function
let cancel: () => void = (): void => {};
const cancelPromise = new Promise<Response>(
(_, reject): void => {
cancel = (): void => {
controller.abort();
reject('canceled');
};
},
);
const cancelPromise = new Promise<Response>((): void => {
cancel = (): void => controller.abort('canceled');
});

const promise = Promise.race([
responsePromise,
Expand Down

0 comments on commit 0a36296

Please sign in to comment.