透明包装异步/同步函数,返回自动重试的版本
返回的函数保持原函数的参数签名,返回值统一为 Promise
要包装的函数(同步或异步)
重试选项
包装后的函数
const fetchUserWithRetry = retry( (id: string) => fetch(`/api/user/${id}`).then(r => r.json()), { attempts: 3, delay: 1000 },);const user = await fetchUserWithRetry('123'); Copy
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),}); Copy
const fetchWithBackoff = retry(fetchData, { attempts: 5, delay: ({ attempt }) => Math.min(1000 * 2 ** (attempt - 1), 30000),});
透明包装异步/同步函数,返回自动重试的版本
返回的函数保持原函数的参数签名,返回值统一为 Promise