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,