# @humanwhocodes/retry 中文文档

规范地址：https://npmcn.com/package/%40humanwhocodes/retry/

版本：0\.4\.3
npm 元数据更新时间：2025-05-07T14:25:57.572Z
中文译文更新时间：2026-11-05T14:41:03.354Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40humanwhocodes/retry
原文对照：https://npmcn.com/package/%40humanwhocodes/retry/?lang=original
项目主页：https://github.com/humanwhocodes/retry#readme
源码仓库：https://github.com/humanwhocodes/retry

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：65.7 kB，9 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=18\.18
- 运行依赖：0 个
- 首次发布：2024-01-17T00:18:36.363Z
- 最新版本发布：2025-05-07T14:25:57.372Z
- 近 30 天下载量：528408397（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：15（近 12 个月 0 个）

## 中文简介

重试工具失败 .

## 安装

```sh
npm install @humanwhocodes/retry
```

也可以使用：`pnpm add @humanwhocodes/retry`、`yarn add @humanwhocodes/retry`、`bun add @humanwhocodes/retry`

## README

# 重试工具

以 [尼古拉斯·扎卡斯](https://humanwhocodes.com/)

如果你觉得有用,请考虑支持我的工作 [捐赠](https://humanwhocodes.com/donate) 或者说 [提名我](https://stars.github.com/nominate/) 为吉他之星.

## 说明

基于返回错误的 Aync JavaScript 调用失败 .

## 使用量

### 节点.js

使用 [无](https://npmjs.com/) 或者说 [线条](https://yarnpkg.com/)数字 :

```
npm install @humanwhocodes/retry

# or

yarn add @humanwhocodes/retry
```

导入到您的节点. js 项目 :

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

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

### 德诺

使用 [日 尔](https://jsr.io/)数字 :

```shell
deno add @humanwhocodes/retry

#or

jsr add @humanwhocodes/retry
```

然后导入您的 Deno 项目 :

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

### 阿宝

使用此命令安装 :

```
bun add @humanwhocodes/retry
```

导入您的 Bun 项目 :

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

### 浏览器

建议导入精密版本以节省带宽:

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

然而,您也可以导入未修改的版本用于调试:

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

## API 密码

导入后创建新实例 `Retrier` 并指定在错误上运行的函数。 此函数应返回 `true` 如果你想再打一次电话 `false` 如果不是的话.

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

那么,打电话给 `retry()` 围绕您想要重试的函数的方法, 例如 :

```js
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"));
```

这个 `retry()` 方法要么在成功时通过结果,要么等待并重试失败。 没有被回转器所捕获的任何错误都会被自动拒绝,所以最终的结果是透明地通过成功和失败.

### 设置超时

您可以控制任务在放弃之前尝试重试的时间 `timeout` 选项 `Retrier` 建筑师。 默认时间为一分.

```js
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"));
```

当调用时, 它拒绝调用函数的第一个错误 .

### 设定货币限额

当处理大量函数调用时,您可以通过通过 `concurrency` 选项 `Retrier` 建筑师。 默认, `concurrency` 这是1000.

```js
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"))
);
```

### 中止为 `AbortSignal`

也可以通过一个 `AbortSignal` 取消重试 :

```js
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 }
);
```

## 开发者设置

1.  叉子存储器
2.  砍断你的叉子
3.  运行 `npm install` 要设置依赖关系
4.  运行 `npm test` 运行测试

### 除错输出

通过设置调试输出 `DEBUG` 环境变量改为 `"@hwc/retry"` 运行前.

## 许可证

Apache 2.0 (英语)

## 上一个艺术

此工具的灵感来自, 包含代码来自 [`graceful-fs`](https://github.com/isaacs/node-graceful-fs)。 。 。 .

## 常见问题

### @humanwhocodes/retry 是什么？

重试工具失败 \.

### 如何安装 @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 和提交记录判断。

