Open History to see the last commit
1---
2id: asyncRetry
3title: asyncRetry
4---
5
6```ts
7function asyncRetry<TFn>(fn, initialOptions?): (...args) => Promise<Awaited<ReturnType<TFn>> | undefined>;
8```
9
10Defined in: [async-retryer.ts:667](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-retryer.ts#L667)
11
12Creates a retry-enabled version of an async function. This is a convenience wrapper
13around the AsyncRetryer class that returns the execute method.
14
15## Type Parameters
16
17### TFn
18
19`TFn` *extends* [`AnyAsyncFunction`](../type-aliases/AnyAsyncFunction.md)
20
21## Parameters
22
23### fn
24
25`TFn`
26
27The async function to add retry functionality to
28
29### initialOptions?
30
31[`AsyncRetryerOptions`](../interfaces/AsyncRetryerOptions.md)\<`TFn`\> = `{}`
32
33Configuration options for the retry behavior
34
35## Returns
36
37A new function that executes the original with retry logic
38
39(...`args`) => `Promise`\<`Awaited`\<`ReturnType`\<`TFn`\>\> \| `undefined`\>
40
41## Example
42
43```typescript
44// Define your async function normally
45async function fetchData(url: string) {
46 const response = await fetch(url)
47 if (!response.ok) throw new Error('Request failed')
48 return response.json()
49}
50
51// Create retry-enabled function
52const fetchWithRetry = asyncRetry(fetchData, {
53 maxAttempts: 3,
54 backoff: 'exponential',
55 baseWait: 1000,
56 jitter: 0.1
57})
58
59// Call it multiple times
60const data1 = await fetchWithRetry('/api/data1')
61const data2 = await fetchWithRetry('/api/data2')
62```