@humanwhocodes/retry

中文文档
0.4.3发布于 2025/05/07Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 5.3亿

A utility to retry failed async methods.

原始使用文档
文档目录14 节
  1. Description
  2. Usage
  3. Node.js
  4. Deno
  5. Bun
  6. Browser
  7. API
  8. Setting a Timeout
  9. Setting a Concurrency Limit
  10. Aborting with AbortSignal
  11. Developer Setup
  12. Debug Output
  13. License
  14. Prior Art

Retry utility

by Nicholas C. Zakas

If you find this useful, please consider supporting my work with a donation or nominate me for a GitHub Star.

Description

A utility for retrying failed async JavaScript calls based on the error returned.

Usage

Node.js

Install using npm or yarn:

npm install @humanwhocodes/retry

# or

yarn add @humanwhocodes/retry

Import into your Node.js project:

// CommonJS
const { Retrier } = require("@humanwhocodes/retry");

// ESM
import { Retrier } from "@humanwhocodes/retry";

Deno

Install using JSR:

deno add @humanwhocodes/retry

#or

jsr add @humanwhocodes/retry

Then import into your Deno project:

import { Retrier } from "@humanwhocodes/retry";

Bun

Install using this command:

bun add @humanwhocodes/retry

Import into your Bun project:

import { Retrier } from "@humanwhocodes/retry";

Browser

It's recommended to import the minified version to save bandwidth:

import { Retrier } from "https://cdn.skypack.dev/@humanwhocodes/retry?min";

However, you can also import the unminified version for debugging purposes:

import { Retrier } from "https://cdn.skypack.dev/@humanwhocodes/retry";

API

After importing, create a new instance of Retrier and specify the function to run on the error. This function should return true if you want the call retried and false if not.

// this instance will retry if the specific error code is found
const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
});

Then, call the retry() method around the function you'd like to retry, such as:

import fs from "fs/promises";

const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
});

const text = await retrier.retry(() => fs.readFile("README.md", "utf8"));

The retry() method will either pass through the result on success or wait and retry on failure. Any error that isn't caught by the retrier is automatically rejected so the end result is a transparent passing through of both success and failure.

Setting a Timeout

You can control how long a task will attempt to retry before giving up by passing the timeout option to the Retrier constructor. By default, the timeout is one minute.

import fs from "fs/promises";

const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
}, { timeout: 100_000 });

const text = await retrier.retry(() => fs.readFile("README.md", "utf8"));

When a call times out, it rejects the first error that was received from calling the function.

Setting a Concurrency Limit

When processing a large number of function calls, you can limit the number of concurrent function calls by passing the concurrency option to the Retrier constructor. By default, concurrency is 1000.

import fs from "fs/promises";

const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
}, { concurrency: 100 });

const filenames = getFilenames();
const contents = await Promise.all(
    filenames.map(filename => retrier.retry(() => fs.readFile(filename, "utf8"))
);

Aborting with AbortSignal

You can also pass an AbortSignal to cancel a retry:

import fs from "fs/promises";

const controller = new AbortController();
const retrier = new Retrier(error => {
    return error.code === "ENFILE" || error.code === "EMFILE";
});

const text = await retrier.retry(
    () => fs.readFile("README.md", "utf8"),
    { signal: controller.signal }
);

Developer Setup

  1. Fork the repository
  2. Clone your fork
  3. Run npm install to setup dependencies
  4. Run npm test to run tests

Debug Output

Enable debugging output by setting the DEBUG environment variable to "@hwc/retry" before running.

License

Apache 2.0

Prior Art

This utility is inspired by, and contains code from graceful-fs.

版本与发布记录

@humanwhocodes/retry 共发布 15 个版本,首次发布于 2024/01/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.3latest2025/05/071.5 年前
0.4.22025/02/201.7 年前
0.4.12024/11/062 年前
0.4.02024/11/012 年前
0.3.12024/10/052 年前
0.3.02024/05/102 年前

关于 @humanwhocodes/retry 的常见问题

@humanwhocodes/retry 是什么?

A utility to retry failed async methods.

如何安装 @humanwhocodes/retry?

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

@humanwhocodes/retry 支持 TypeScript 吗?

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

@humanwhocodes/retry 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

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

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

@humanwhocodes/retry 可以商用吗?

@humanwhocodes/retry 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

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

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

@humanwhocodes/retry 还在维护吗?

最新版本 0.4.3 发布于 2025/05/07(1.5 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/01/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。