diff --git a/src/common.ts b/src/common.ts index 2915b27..4513cdc 100644 --- a/src/common.ts +++ b/src/common.ts @@ -159,6 +159,13 @@ export interface RetryConfig { * When there is no response, the number of retries to attempt. Defaults to 2. */ noResponseRetries?: number; + + /** + * Function to invoke which returns a promise. After the promise resolves, + * the retry will be triggered. If provided, this will be used in-place of + * the `retryDelay` + */ + retryBackoff?: (err: GaxiosError, defaultBackoffMs: number) => Promise; } export type FetchImplementation = ( diff --git a/src/retry.ts b/src/retry.ts index a4462a9..540298e 100644 --- a/src/retry.ts +++ b/src/retry.ts @@ -70,9 +70,11 @@ export async function getRetryConfig(err: GaxiosError) { err.config.retryConfig!.currentRetryAttempt! += 1; // Create a promise that invokes the retry after the backOffDelay - const backoff = new Promise(resolve => { - setTimeout(resolve, delay); - }); + const backoff = config.retryBackoff + ? config.retryBackoff(err, delay) + : new Promise(resolve => { + setTimeout(resolve, delay); + }); // Notify the user if they added an `onRetryAttempt` handler if (config.onRetryAttempt) {