Skip to content

async.Function.retry

github-actions[bot] edited this page Jun 8, 2026 · 2 revisions

@zenstone/ts-utils / async / retry

Function: retry()

retry<F>(fn, options?): (...args) => Promise<Awaited<ReturnType<F>>>

Defined in: src/async/index.ts:166

透明包装异步/同步函数,返回自动重试的版本

返回的函数保持原函数的参数签名,返回值统一为 Promise

Type Parameters

F

F extends AnyFn

Parameters

fn

F

要包装的函数(同步或异步)

options?

RetryOptions = {}

重试选项

Returns

包装后的函数

(...args) => Promise<Awaited<ReturnType<F>>>

Examples

const fetchUserWithRetry = retry(
  (id: string) => fetch(`/api/user/${id}`).then(r => r.json()),
  { attempts: 3, delay: 1000 },
);

const user = await fetchUserWithRetry('123');

指数退避

const fetchWithBackoff = retry(fetchData, {
  attempts: 5,
  delay: ({ attempt }) => Math.min(1000 * 2 ** (attempt - 1), 30000),
});

Clone this wiki locally