@ambassify/fetch-retried

中文文档
2.2.0发布于 2025/10/02MITCommonJS近 30 天下载 1923

Use the fetch API to run requests, implementing retries

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Options
  4. Backoff strategies
  5. Contribute
  6. License
  7. About us

fetch-retried

Use the fetch API to run requests, implementing retries

Installation

npm install --save @ambassify/fetch-retried

Usage

You can use the fetch api as per usual. The only thing different is the source of your fetch method. Here you use fetchRetried to create a fetch method that will use your desired config.

const fetchRetried = require('@ambassify/fetch-retried');
const fetch = fetchRetried({ delay: 100 });

fetch('https://www.google.com')
    .then(resp => resp.json())
    .then(json => console.log(json));

Options

All options are optional and have default values.

  • delay: When using the default exponential backoff, the delay used to calculate the timeout. Otherwise a method that calculated the timeout used. (default: 200)
  • retries: The number of times to retry a request. (default: 5)
  • isOK: A method that determines whether a request succeeded by returning true or false when passed a response. (default: (resp) => resp.ok)
  • shouldRetryError: When fetch throws an error this method determines whether the request is retried by returning true or false (default: () => true)
  • retryMethods: Which HTTP verbs to retry (default: ['PUT', 'DELETE', 'GET', 'HEAD', 'PATCH', 'OPTIONS']).
  • shouldRetry: Allows you to override the default method to check if a retry should be tried. Takes a function that accepts the following signature: shouldRetry(url, requestOptions, { attempts, config, error, response }).
  • fetch: The underlying fetch implementation to use. (default: require('node-fetch'))

Backoff strategies

Strategies are attached to the default import of this package and can be accessed using.

const fetchRetried = require('@ambassify/fetch-retried');

fetchRetried.exponential;
// OR
fetchRetried.binaryExponential;

// Usage:
const fetch = fetchRetried({
    delay: fetchRetried.exponential(10)
})
// OR
const fetch = fetchRetried({
    delay: fetchRetried.binaryExponential()
})

exponential

function exponential(delay) {
    return (attempts) => (attempts * attempts) * delay;
}

binaryExponential

function binaryExponential(delay = 1) {
    return (attempts) => (Math.pow(2, attempts) - 1) * delay;
}

Contribute

We really appreciate any contribution you would like to make, so don't hesitate to report issues or submit pull requests.

License

This project is released under a MIT license.

About us

If you would like to know more about us, be sure to have a look at our website, or our Twitter accounts Ambassify, Sitebase, JorgenEvens

版本与发布记录

@ambassify/fetch-retried 共发布 12 个版本,首次发布于 2017/01/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.0latest2025/10/021.1 年前
2.1.02025/10/021.1 年前
2.0.52025/10/021.1 年前
2.0.42023/02/033 年前
2.0.32021/09/025 年前
2.0.22020/04/306 年前

关于 @ambassify/fetch-retried 的常见问题

@ambassify/fetch-retried 是什么?

Use the fetch API to run requests, implementing retries

如何安装 @ambassify/fetch-retried?

在项目目录运行 npm install @ambassify/fetch-retried,或使用 pnpm add / yarn add / bun add。

@ambassify/fetch-retried 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ambassify__fetch-retried 是否存在。

@ambassify/fetch-retried 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ambassify/fetch-retried 可以商用吗?

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

@ambassify/fetch-retried 的下载量有多少?

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

@ambassify/fetch-retried 还在维护吗?

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