diff --git a/src/index.ts b/src/index.ts index a42662f..594645b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,12 @@ export interface RetryConfig { /** * Function to invoke when a retry attempt is made. */ - onRetryAttempt?: (error: AxiosError) => void; + onError?: (error: AxiosError) => void | Promise; + + /** + * Function to invoke when a retry attempt is made. + */ + onRetryAttempt?: (error: AxiosError) => void | Promise; /** * Function to invoke which determines if you should retry @@ -285,17 +290,21 @@ async function onError(instance: AxiosInstance, error: AxiosError) { setTimeout(resolve, delay); }); - // Notify the user if they added an `onRetryAttempt` handler - if (config.onRetryAttempt) { - config.onRetryAttempt(axiosError); + if (config.onError) { + await config.onError(axiosError); } - const onRetryAttemptPromise = Promise.resolve(); + // Notify the user if they added an `onRetryAttempt` handler + const onRetryAttemptPromise = async () => { + if (config.onRetryAttempt) { + await config.onRetryAttempt(axiosError); + } + } // 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!)); } diff --git a/test/index.ts b/test/index.ts index f9b32f3..179e809 100644 --- a/test/index.ts +++ b/test/index.ts @@ -630,7 +630,7 @@ describe('retry-axios', () => { const axiosPromise = axios({ url, raxConfig: { - onRetryAttempt: resolve, + onError: resolve, retryDelay: 10_000, // Higher default to ensure Retry-After is used backoffType: 'static', }, @@ -660,7 +660,7 @@ describe('retry-axios', () => { const axiosPromise = axios({ url, raxConfig: { - onRetryAttempt: resolve, + onError: resolve, backoffType: 'static', retryDelay: 10_000, }, @@ -710,7 +710,7 @@ describe('retry-axios', () => { const axiosPromise = axios({ url, raxConfig: { - onRetryAttempt: resolve, + onError: resolve, retryDelay: 10_000, // Higher default to ensure maxRetryDelay is used maxRetryDelay: 5000, backoffType: 'exponential',