retry function

Retry an async operation with exponential backoff.

Example

const data = await retry(
  () => fetch('/api/data'),
  3,
  100
);

Signature

retry<T>(operation: () => Promise<T>, maxAttempts: number, backoffMs: number): Promise<T>

Type Parameters

T

The return type of the operation

Parameters

operation

Async function to retry

maxAttempts

Maximum number of attempts

backoffMs

Initial backoff delay in milliseconds

Returns

Promise resolving to the operation result