-
Notifications
You must be signed in to change notification settings - Fork 0
async.Function.retryFn
github-actions[bot] edited this page Jun 8, 2026
·
2 revisions
@zenstone/ts-utils / async / retryFn
retryFn<
T,Args>(fn,options?): (...args) =>Promise<Awaited<T>>
Defined in: src/async/index.ts:198
创建感知重试状态的函数
fn 的第一个参数为 RetryFnParams,包含 attempt 和 options, 允许根据重试次数做差异化处理。返回的函数剥掉 RetryFnParams, 只暴露业务参数。
T
Args extends unknown[]
RetryCallbackFn<T, Args>
接收 RetryFnParams 的回调函数
RetryOptions = {}
重试选项
只保留业务参数的包装函数
(...args) => Promise<Awaited<T>>
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');