-
Notifications
You must be signed in to change notification settings - Fork 0
async.Function.retry
github-actions[bot] edited this page Jun 8, 2026
·
2 revisions
@zenstone/ts-utils / async / retry
retry<
F>(fn,options?): (...args) =>Promise<Awaited<ReturnType<F>>>
Defined in: src/async/index.ts:166
透明包装异步/同步函数,返回自动重试的版本
返回的函数保持原函数的参数签名,返回值统一为 Promise
F extends AnyFn
F
要包装的函数(同步或异步)
RetryOptions = {}
重试选项
包装后的函数
(...args) => Promise<Awaited<ReturnType<F>>>
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),
});