Skip to content

Commit

Permalink
fix: added check for cancel tokens (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRPrince authored May 5, 2020
1 parent fc8117e commit 734a93f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ function normalizeArray<T>(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 =
Expand Down
31 changes: 31 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 734a93f

Please sign in to comment.