Skip to main content

Function: withRetry()

withRetry<T>(operation, config?): Promise<RetryResult<T>>

Defined in: packages/core/src/utils/retry.ts:223

Execute an async operation with retry logic and exponential backoff

Type Parameters

T

T

Parameters

operation

() => Promise<T>

The async function to execute

config?

RetryConfig = {}

Retry configuration

Returns

Promise<RetryResult<T>>

RetryResult containing success status, value or error, and attempt count

Example

// Basic usage
const result = await withRetry(() => fetchData());
if (result.success) {
console.log('Got data:', result.value);
} else {
console.error('Failed after', result.attempts, 'attempts:', result.error);
}

// With custom configuration
const result = await withRetry(
() => riskyOperation(),
{
maxRetries: 5,
initialDelayMs: 500,
shouldRetry: (error) => error.message.includes('timeout'),
onRetry: (error, attempt, delay) => {
console.log(`Retry ${attempt} in ${delay}ms: ${error.message}`);
},
}
);