@humanwhocodes/retry

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

重试工具失败 .

中文使用文档
文档目录14 节
  1. 说明
  2. 使用量
  3. 节点.js
  4. 德诺
  5. 阿宝
  6. 浏览器
  7. API 密码
  8. 设置超时
  9. 设定货币限额
  10. 中止为 AbortSignal
  11. 开发者设置
  12. 除错输出
  13. 许可证
  14. 上一个艺术

重试工具

以 尼古拉斯·扎卡斯

如果你觉得有用,请考虑支持我的工作 捐赠 或者说 提名我 为吉他之星.

说明

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

使用量

节点.js

使用 无 或者说 线条数字 :

npm install @humanwhocodes/retry

# or

yarn add @humanwhocodes/retry

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

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

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

德诺

使用 日 尔数字 :

deno add @humanwhocodes/retry

#or

jsr add @humanwhocodes/retry

然后导入您的 Deno 项目 :

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

阿宝

使用此命令安装 :

bun add @humanwhocodes/retry

导入您的 Bun 项目 :

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

浏览器

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

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

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

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

API 密码

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

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

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

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 建筑师。 默认时间为一分.

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.

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 取消重试 :

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。 。 。 .

版本与发布记录

@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 是什么?

重试工具失败 .

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