@meld-ts/core
    Preparing search index...

    Function retry

    • 透明包装异步/同步函数,返回自动重试的版本

      返回的函数保持原函数的参数签名,返回值统一为 Promise

      Type Parameters

      Parameters

      • fn: F

        要包装的函数(同步或异步)

      • options: RetryOptions = {}

        重试选项

      Returns (...args: Parameters<F>) => 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),
      });