From 734a93ff45de7dfd827f17ec7e7545636a3e8add Mon Sep 17 00:00:00 2001 From: Aaron Prince <45446692+AaronRPrince@users.noreply.github.com> Date: Tue, 5 May 2020 11:20:17 -0500 Subject: [PATCH] fix: added check for cancel tokens (#99) --- src/index.ts | 4 ++++ test/index.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/index.ts b/src/index.ts index 74b5871..a9b81d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,6 +125,10 @@ function normalizeArray(obj?: T[]): T[] | undefined { } function onError(err: AxiosError) { + if (axios.isCancel(err)) { + return Promise.reject(err); + } + const config = getConfig(err) || {}; config.currentRetryAttempt = config.currentRetryAttempt || 0; config.retry = diff --git a/test/index.ts b/test/index.ts index e7c2bfd..48fcead 100644 --- a/test/index.ts +++ b/test/index.ts @@ -298,4 +298,35 @@ describe('retry-axios', () => { assert.strictEqual(res2.data, 'toast'); scopes.forEach(s => s.done()); }); + + it('should ignore requests that have been canceled', async () => { + const scopes = [ + nock(url) + .get('/') + .times(2) + .delay(5) + .reply(500), + nock(url) + .get('/') + .reply(200, 'toast'), + ]; + interceptorId = rax.attach(); + try { + const src = axios.CancelToken.source(); + const cfg: rax.RaxConfig = { + url, + raxConfig: { retry: 2 }, + cancelToken: src.token, + }; + const req = axios(cfg); + setTimeout(() => { + src.cancel(); + }, 10); + const res = await req; + throw new Error('The canceled request completed.'); + } catch (err) { + assert.strictEqual(axios.isCancel(err), true); + } + assert.strictEqual(scopes[1].isDone(), false); + }); });