Skip to content

Commit

Permalink
Merge pull request #1722 from ably/1623/fix-abort-controller-in-fetch…
Browse files Browse the repository at this point in the history
…-request

[ECO-4113] Add missing `signal` option when using AbortController with fetch API
  • Loading branch information
VeskeR authored Apr 17, 2024
2 parents d0a43d1 + d418952 commit 0c0021a
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/platform/web/lib/http/request/fetchrequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export default async function fetchRequest(
timeout = setTimeout(
() => {
controller.abort();
// When AbortController.abort() is called, the fetch() promise rejects with a DOMException named AbortError (source: https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
// However, we beat it in the Promise.race() by resolving our custom 'Request timed out' error on the next line, thereby exposing users to the better-formatted error.
resolve({ error: new PartialErrorInfo('Request timed out', null, 408) });
},
client ? client.options.timeouts.httpRequestTimeout : Defaults.TIMEOUTS.httpRequestTimeout,
Expand All @@ -54,6 +56,7 @@ export default async function fetchRequest(
method: _method,
headers: fetchHeaders,
body: body as any,
signal: controller.signal,
};

if (!Platform.Config.isWebworker) {
Expand Down

0 comments on commit 0c0021a

Please sign in to comment.