File: AsyncRetryer.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
React
Version
Latest
Search...
+ K
Menu
Getting Started
Guides
API Reference
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference
Debouncer Examples
Throttler Examples
Rate Limiter Examples
Queue Examples
Batcher Examples
TanStack Query Examples
Framework
React
Version
Latest
Menu
Getting Started
Guides
API Reference
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference
Debouncer Examples
Throttler Examples
Rate Limiter Examples
Queue Examples
Batcher Examples
TanStack Query Examples
On this page
Copy Markdown
Class: AsyncRetryer<TFn>
========================
Defined in: async-retryer.ts:288
Provides robust retry functionality for asynchronous functions, supporting configurable backoff strategies, attempt limits, timeout controls, and detailed state management. The AsyncRetryer class is designed to help you reliably execute async operations that may fail intermittently, such as network requests or database operations, by automatically retrying them according to your chosen policy.
Retrying Concepts
-----------------
State Management
----------------
Uses TanStack Store for fine-grained reactivity. State can be accessed via the store.state property.
Available state properties:
The throwOnError option controls when errors are thrown (default: 'last'):
Callbacks for lifecycle management:
Important: This class is designed for single-use execution. Calling execute() multiple times on the same instance will abort previous executions. For multiple calls, create a new instance each time.
typescript
// Retry a fetch operation up to 5 times with exponential backoff, jitter, and timeouts
const retryer = new AsyncRetryer(async (url: string) => {
const signal = retryer.getAbortSignal()
return await fetch(url, { signal })
}, {
maxAttempts: 5,
backoff: 'exponential',
baseWait: 1000,
jitter: 0.1, // Add 10% random variation to prevent thundering herd
maxExecutionTime: 5000, // Abort individual calls after 5 seconds
maxTotalExecutionTime: 30000, // Abort entire operation after 30 seconds
onRetry: (attempt, error) => console.log(`Retry attempt ${attempt} after error:`, error),
onSuccess: (result) => console.log('Success:', result),
onError: (error) => console.error('Error:', error),
onLastError: (error) => console.error('All retries failed:', error),
})
const result = await retryer.execute('/api/data')
// Retry a fetch operation up to 5 times with exponential backoff, jitter, and timeouts
const retryer = new AsyncRetryer(async (url: string) => {
const signal = retryer.getAbortSignal()
return await fetch(url, { signal })
}, {
maxAttempts: 5,
backoff: 'exponential',
baseWait: 1000,
jitter: 0.1, // Add 10% random variation to prevent thundering herd
maxExecutionTime: 5000, // Abort individual calls after 5 seconds
maxTotalExecutionTime: 30000, // Abort entire operation after 30 seconds
onRetry: (attempt, error) => console.log(`Retry attempt ${attempt} after error:`, error),
onSuccess: (result) => console.log('Success:', result),
onError: (error) => console.error('Error:', error),
onLastError: (error) => console.error('All retries failed:', error),
})
const result = await retryer.execute('/api/data')
Type Parameters
---------------
### TFn
TFn extends AnyAsyncFunction
The async function type to be retried.
Constructors
------------
### Constructor
ts
new AsyncRetryer<TFn>(fn, initialOptions): AsyncRetryer<TFn>;
new AsyncRetryer<TFn>(fn, initialOptions): AsyncRetryer<TFn>;
Defined in: async-retryer.ts:301
Creates a new AsyncRetryer instance
TFn
The async function to retry
AsyncRetryerOptions <TFn> = {}
Configuration options for the retryer
AsyncRetryer<TFn>
ts
fn: TFn;
fn: TFn;
Defined in: async-retryer.ts:302
The async function to retry
ts
key: string | undefined;
key: string | undefined;
Defined in: async-retryer.ts:292
ts
options: AsyncRetryerOptions<TFn> & Omit<Required<AsyncRetryerOptions<any>>,
| "initialState"
| "onError"
| "onSettled"
| "onSuccess"
| "key"
| "onAbort"
| "onLastError"
| "onRetry"
| "onExecutionTimeout"
| "onTotalExecutionTimeout">;
options: AsyncRetryerOptions<TFn> & Omit<Required<AsyncRetryerOptions<any>>,
| "initialState"
| "onError"
| "onSettled"
| "onSuccess"
| "key"
| "onAbort"
| "onLastError"
| "onRetry"
| "onExecutionTimeout"
| "onTotalExecutionTimeout">;
Defined in: async-retryer.ts:293
ts
readonly store: Store<Readonly<AsyncRetryerState<TFn>>>;
readonly store: Store<Readonly<AsyncRetryerState<TFn>>>;
Defined in: async-retryer.ts:289
ts
abort(reason): void;
abort(reason): void;
Defined in: async-retryer.ts:603
Cancels the current execution and any pending retries
The reason for the abort (defaults to 'manual')
"manual" | "execution-timeout" | "total-timeout" | "new-execution"
void
ts
execute(...args): Promise<Awaited<ReturnType<TFn>> | undefined>;
execute(...args): Promise<Awaited<ReturnType<TFn>> | undefined>;
Defined in: async-retryer.ts:410
Executes the function with retry logic
...Parameters<TFn>
Arguments to pass to the function
Promise<Awaited<ReturnType<TFn>> | undefined>
The function result, or undefined if disabled or all retries failed (when throwOnError is false)
The last error if throwOnError is true and all retries fail
ts
getAbortSignal(): AbortSignal | null;
getAbortSignal(): AbortSignal | null;
Defined in: async-retryer.ts:595
Returns the current AbortSignal for the executing operation. Use this signal in your async function to make it cancellable. Returns null when not currently executing.
AbortSignal | null
typescript
const retryer = new AsyncRetryer(async (userId: string) => {
const signal = retryer.getAbortSignal()
if (signal) {
return fetch(`/api/users/${userId}`, { signal })
}
return fetch(`/api/users/${userId}`)
})
// Abort will now actually cancel the fetch
retryer.abort()
const retryer = new AsyncRetryer(async (userId: string) => {
const signal = retryer.getAbortSignal()
if (signal) {
return fetch(`/api/users/${userId}`, { signal })
}
return fetch(`/api/users/${userId}`)
})
// Abort will now actually cancel the fetch
retryer.abort()
ts
reset(): void;
reset(): void;
Defined in: async-retryer.ts:623
Resets the retryer to its initial state
void
ts
setOptions(newOptions): void;
setOptions(newOptions): void;
Defined in: async-retryer.ts:326
Updates the retryer options
#### Parameters ##### newOptions
Partial<AsyncRetryerOptions <TFn>>
Partial options to merge with existing options
void
