From 10f6dbc816d6f07a81d49945ec0291b132a75fb7 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Tue, 19 Dec 2023 14:24:47 +0330 Subject: [PATCH] fix(fetch): AbortController not support issue --- packages/fetch/src/fetch.ts | 38 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/packages/fetch/src/fetch.ts b/packages/fetch/src/fetch.ts index b127664..f4e18bf 100644 --- a/packages/fetch/src/fetch.ts +++ b/packages/fetch/src/fetch.ts @@ -33,8 +33,7 @@ export async function serviceRequest): Promise): Promise< } return response.clone(); - } - catch (err) { + } catch (err) { // clean cache on any error. delete duplicateRequestStorage[cacheKey]; throw err; @@ -295,8 +289,7 @@ async function _handleRetryPattern(options: Required): Promise { // else logger.logMethod?.('_handleTimeout'); return new Promise((resolved, reject) => { - // TODO: AbortController polyfill - const abortController = new AbortController(); + const abortController = typeof globalScope.AbortController === 'function' ? new AbortController() : null; const externalAbortSignal = options.signal; - options.signal = abortController.signal; + options.signal = abortController?.signal; - const timeoutId = setTimeout(() => { - reject(new Error('fetch_timeout')); - abortController.abort('fetch_timeout'); - }, options.timeout); - - if (externalAbortSignal != null) { + if (abortController !== null && externalAbortSignal != null) { // Respect external abort signal externalAbortSignal.addEventListener('abort', () => abortController.abort(), {once: true}); } + const timeoutId = setTimeout(() => { + reject(new Error('fetch_timeout')); + abortController?.abort('fetch_timeout'); + }, options.timeout); + // abortController.signal.addEventListener('abort', () => { // logger.incident('fetch', 'fetch_abort_signal', { // reason: abortController.signal.reason,