@checkdigit/retry

中文文档
8.0.0发布于 2026/08/29MITTypeScriptES Module构建来源已验证近 30 天下载 6467

Implementation of the standard Check Digit retry algorithm

原始使用文档
文档目录4 节
  1. Installing
  2. Use
  3. Using with @checkdigit/timeout
  4. License

checkdigit/retry

Copyright © 2021-2026 Check Digit, LLC

The @checkdigit/retry module implements the recommended Check Digit retry algorithm for idempotent distributed work.

The default recommended behavior for production usage is to retry up to eight times with exponential backoff. Before retry number n (starting at 1), the maximum backoff is (2^(n - 1) * 100) milliseconds, and full jitter selects a delay between zero and that maximum. With the default eight retries, the maximum delays are 100, 200, 400, 800, 1,600, 3,200, 6,400, and 12,800 milliseconds. This is at most 25.5 seconds of total backoff, or approximately 12.75 seconds on average with full jitter, excluding the execution time of the initial attempt and eight retries. This logic matches the AWS recommended algorithm and AWS exponential backoff and jitter doc.

However, default waitRatio (100), retries (8), jitter (true) and maximumBackoff (+Infinity) can be overridden. For test scenarios, it is useful to set the waitRatio to 0 to force immediate retries.

If the number of allowable retries is exceeded, a RetryError is thrown with the options property set to the input options, and cause set to the error thrown on the last retry attempt.

NOTE: this module assumes all work is idempotent, and can be retried multiple times without consequence. If that is not the case, do not use this module.

Installing

npm install @checkdigit/retry

Use

import retry from '@checkdigit/retry';

// do some simple work, with the default of 8 retries and a wait ratio of 100.
const worker = retry(async (item: string) => {
  /* idempotent network requests */
});
const result = await worker(someInput);

// Do some simple work, with a wait ratio of 0.  This means retries occur immediately, useful for test scenarios.
const testWorker = retry(
  async (item: string) => {
    /* ...idempotent network requests... */
  },
  { waitRatio: 0 },
);
const testResult = await testWorker(someTestInput);

// catch a RetryError
const errorWorker = retry(
  async (item: string) => {
    /* ...repeated failures... */
  },
  { waitRatio: 0 },
);
try {
  await errorWorker(someTestInput);
} catch (error) {
  if (error instanceof RetryError) {
    console.log('failed after ' + error.options.retries);
    console.log(error.cause);
  }
}

Using with @checkdigit/timeout

In some scenarios, it is recommended that this module is combined with @checkdigit/timeout.

import retry from '@checkdigit/retry';
import timeout from '@checkdigit/timeout';

// wait up to 60 seconds on each attempt, retry on timeout
const worker = await retry((item) =>
  timeout(
    (async (input) => {
      /* ...work... */
    })(item),
  ),
);
const result = await worker(someInput);

License

MIT

版本与发布记录

@checkdigit/retry 共发布 26 个版本,首次发布于 2021/03/05;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.0.0latest2026/08/292 个月前
8.0.0-PR.28-aa3ebeta2026/08/282 个月前
7.0.02025/02/071.8 年前
7.0.0-PR.26-a4dc2025/02/061.8 年前
7.0.0-PR.26-b1ee2025/02/061.8 年前
6.0.02024/04/092 年前

关于 @checkdigit/retry 的常见问题

@checkdigit/retry 是什么?

Implementation of the standard Check Digit retry algorithm

如何安装 @checkdigit/retry?

在项目目录运行 npm install @checkdigit/retry,或使用 pnpm add / yarn add / bun add。

@checkdigit/retry 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@checkdigit/retry 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@checkdigit/retry 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.18.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@checkdigit/retry 可以商用吗?

@checkdigit/retry 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@checkdigit/retry 的下载量有多少?

根据 npm 官方下载统计,@checkdigit/retry 在 2026/10/10 – 2026/11/08 这 30 天内被下载 6,467 次,日均约 216 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@checkdigit/retry 还在维护吗?

最新版本 8.0.0 发布于 2026/08/29(2 个月前);至今共发布 26 个版本,其中过去 12 个月发布了 2 个;首次发布于 2021/03/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。