Skip to content

async.Function.retryFn

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

@zenstone/ts-utils / async / retryFn

Function: retryFn()

retryFn<T, Args>(fn, options?): (...args) => Promise<Awaited<T>>

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

创建感知重试状态的函数

fn 的第一个参数为 RetryFnParams,包含 attempt 和 options, 允许根据重试次数做差异化处理。返回的函数剥掉 RetryFnParams, 只暴露业务参数。

Type Parameters

T

T

Args

Args extends unknown[]

Parameters

fn

RetryCallbackFn<T, Args>

接收 RetryFnParams 的回调函数

options?

RetryOptions = {}

重试选项

Returns

只保留业务参数的包装函数

(...args) => Promise<Awaited<T>>

Example

const fetchWithFallback = retryFn(
  ({ attempt }, id: string) => {
    const url = attempt === 1 ? '/api/primary' : '/api/fallback';
    return fetch(`${url}/${id}`).then(r => r.json());
  },
  { attempts: 3 },
);

const data = await fetchWithFallback('123');

Clone this wiki locally