Skip to content

Commit

Permalink
fix: Call onRetryAttempt *after* backoff timeout
Browse files Browse the repository at this point in the history
The log appears before the timeout, which is misleading.

Also return support for async onRetryAttempt.

Fixes JustinBeckwith#119.
  • Loading branch information
orgads committed Dec 20, 2023
1 parent e888e95 commit a27b34d
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface RetryConfig {
/**
* Function to invoke when a retry attempt is made.
*/
onRetryAttempt?: (error: AxiosError) => void;
onRetryAttempt?: (error: AxiosError) => void | Promise<void>;

/**
* Function to invoke which determines if you should retry
Expand Down Expand Up @@ -286,16 +286,16 @@ async function onError(instance: AxiosInstance, error: AxiosError) {
});

// Notify the user if they added an `onRetryAttempt` handler
if (config.onRetryAttempt) {
config.onRetryAttempt(axiosError);
const onRetryAttemptPromise = async () => {
if (config.onRetryAttempt) {
await config.onRetryAttempt(axiosError);
}
}

const onRetryAttemptPromise = Promise.resolve();

// Return the promise in which recalls axios to retry the request
return Promise.resolve()
.then(async () => onBackoffPromise)
.then(async () => onRetryAttemptPromise)
.then(onRetryAttemptPromise)
.then(async () => config.instance!.request(axiosError.config!));
}

Expand Down

0 comments on commit a27b34d

Please sign in to comment.