retry()
Automatically retry failed operations with configurable strategies.
Syntax
typescript
task.retry(times: number, options?: {
delay?: number;
backoff?: number;
shouldRetry?: (error: any, attempt: number) => boolean | Promise<boolean>;
}): FuturableTask<T>Parameters
times
Maximum number of retry attempts.
options (optional)
delay: Milliseconds to wait between retries (default: 0)backoff: Multiplier for exponential backoff (default: 1)shouldRetry: Function to determine if retry should happen
Return Value
A new FuturableTask<T> with retry logic.
Examples
Basic Retry
typescript
const task = FuturableTask
.fetch('/api/data')
.retry(3); // Retry up to 3 timesFixed Delay
typescript
const task = FuturableTask
.of(() => unreliableOperation())
.retry(5, { delay: 1000 }); // Wait 1s between retriesExponential Backoff
typescript
const task = FuturableTask
.fetch('/api/data')
.retry(5, {
delay: 1000,
backoff: 2
});
// Delays: 1s, 2s, 4s, 8s, 16sConditional Retry
typescript
const task = FuturableTask
.fetch('/api/data')
.retry(3, {
shouldRetry: (error) => {
// Only retry on network errors
return error.name === 'NetworkError';
}
});With Logging
typescript
const task = FuturableTask
.of(() => apiCall())
.retry(3, {
delay: 1000,
shouldRetry: (error, attempt) => {
console.log(`Attempt ${attempt} failed:`, error);
return attempt < 3;
}
});