创建感知重试状态的函数
fn 的第一个参数为 RetryFnParams,包含 attempt 和 options, 允许根据重试次数做差异化处理。返回的函数剥掉 RetryFnParams, 只暴露业务参数。
接收 RetryFnParams 的回调函数
重试选项
只保留业务参数的包装函数
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'); Copy
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');
创建感知重试状态的函数
fn 的第一个参数为 RetryFnParams,包含 attempt 和 options, 允许根据重试次数做差异化处理。返回的函数剥掉 RetryFnParams, 只暴露业务参数。