From a27b34db6f2291da23d0e5416e4b3456c5b23694 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Wed, 20 Dec 2023 14:42:13 +0200 Subject: [PATCH] fix: Call onRetryAttempt *after* backoff timeout The log appears before the timeout, which is misleading. Also return support for async onRetryAttempt. Fixes #119. --- src/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index a42662f..3bf391e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; /** * Function to invoke which determines if you should retry @@ -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!)); }