Skip to content

Commit

Permalink
Handle case when abort happends during onRetry
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-billtech committed Jun 4, 2024
1 parent 9aa2ba9 commit d79aba6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
43 changes: 37 additions & 6 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,44 @@ describe('axiosRetry(axios, { retries, retryCondition })', () => {
});

describe('when request is aborted', () => {
it('should clearTimeout', (done) => {
const client = axios.create({
timeout: 100000
it('should clear timeout if started', (done) => {
const client = axios.create();
setupResponses(client, [
() => nock('http://example.com').get('/test').reply(429),
() => nock('http://example.com').get('/test').reply(429)
]);
axiosRetry(client, {
retries: 1,
retryCondition: () => true,
retryDelay: () => 10000
});
setupResponses(client, [() => nock('http://example.com').get('/test').reply(429)]);
const abortController = new AbortController();
client
.get('http://example.com/test', { signal: abortController.signal })
.then(
() => done.fail(),
(error) => {
expect(error).toBeInstanceOf(CanceledError);
expect(new Date().getTime() - timeStart).toBeLessThan(300);
done();
}
)
.catch(done.fail);
setTimeout(() => abortController.abort(), 200);
const timeStart = new Date().getTime();
});

it('should not start a timeout if not started yet', (done) => {
const client = axios.create();
setupResponses(client, [
() => nock('http://example.com').get('/test').reply(429),
() => nock('http://example.com').get('/test').reply(429)
]);
axiosRetry(client, {
retries: 1,
retryCondition: () => true,
retryDelay: () => 9999999
retryDelay: () => 10000,
onRetry: async () => new Promise((resolve) => setTimeout(resolve, 100))
});
const abortController = new AbortController();
client
Expand All @@ -345,11 +374,13 @@ describe('axiosRetry(axios, { retries, retryCondition })', () => {
() => done.fail(),
(error) => {
expect(error).toBeInstanceOf(CanceledError);
expect(new Date().getTime() - timeStart).toBeLessThan(300);
done();
}
)
.catch(done.fail);
abortController.abort();
setTimeout(() => abortController.abort(), 50);
const timeStart = new Date().getTime();
});
});
});
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,17 @@ async function handleRetry(
}
config.transformRequest = [(data) => data];
await onRetry(currentState.retryCount, error, config);
return new Promise((resolve) => {
if (config.signal?.aborted) {
return Promise.resolve(axiosInstance(config));
}
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => resolve(axiosInstance(config)), delay);
if (config.signal?.addEventListener) {
config.signal.addEventListener(
'abort',
() => {
clearTimeout(timeout);
resolve(axiosInstance(config));
},
{ once: true }
);
Expand Down

0 comments on commit d79aba6

Please sign in to comment.