asyncRetry
The helpers useAsyncRetry() and createAsyncRetry() rerun an async function as long as a retry criterion is true, with options for attempts, delay, and logging.
Interactive example
Syntax
typescript
interface CreateAsyncRetryOptions {
maxRetry: number;
timeToSleep?: number;
log?: boolean;
}
function useAsyncRetry<
GenericOutput extends unknown
>(
retryFunction: () => Promise<GenericOutput>,
shouldRetry: (result: GenericOutput) => boolean,
options: CreateAsyncRetryOptions
): Promise<GenericOutput>;
function createAsyncRetry<
GenericAnyFunction extends ((...args: any[]) => Promise<any>)
>(
retryFunction: GenericAnyFunction,
checkFunction: (result: Awaited<ReturnType<GenericAnyFunction>>) => boolean,
options: CreateAsyncRetryOptions
): GenericAnyFunction;Parameters
retryFunction: Async function to retry.shouldRetry/checkFunction: Decides whether another attempt is needed from the previous result.options: Retry options (maxRetry,timeToSleep,log).
Return value
useAsyncRetry: A promise resolved with the last result (after success or exhaustion of retries).createAsyncRetry: A decorated function that applies retry logic on each call.
See also
sleep- Async pause used in some retry scenarios
