axios-retry插件-axios请求失败自动重试

这篇具有很好参考价值的文章主要介绍了axios-retry插件-axios请求失败自动重试。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

介绍

axios-retry 对外导出 axiosRetry() 方法: 通过对 axios 单例添加“拦截器”,来扩展实现自动重试网络请求功能。

安装

npm install axios-retry

使用

// CommonJS
// const axiosRetry = require('axios-retry');

// ES6
import axiosRetry from 'axios-retry';

axiosRetry(axios, { retries: 3 });

axios.get('http://example.com/test') // The first request fails and the second returns 'ok'
  .then(result => {
    result.data; // 'ok'
  });

// Exponential back-off retry delay between requests
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay});

// Custom retry delay
axiosRetry(axios, { retryDelay: (retryCount) => {
  return retryCount * 1000;
}});

// 自定义 axios 实例
const client = axios.create({ baseURL: 'http://example.com' });
axiosRetry(client, { retries: 3 });

client.get('/test') // 第一次请求失败,第二次成功
  .then(result => {
    result.data; // 'ok'
  });

// 允许 request-specific 配置
client
  .get('/test', {
    'axios-retry': {
      retries: 0
    }
  })
  .catch(error => { // The first request fails
    error !== undefined
  });

备注: 除非 shouldResetTimeout被设置, 这个插件
将请求超时解释为全局值, 不是针对每一个请求,二是全局的设置

配置

Name Type Default Description
retries Number 3 The number of times to retry before failing.
retryCondition Function isNetworkOrIdempotentRequestError 如果应该重试请求,则进一步控制的回调。默认情况下,如果是幂等请求的网络错误或5xx错误,它会重试(GET, HEAD, OPTIONS, PUT or DELETE).
shouldResetTimeout Boolean false Defines if the timeout should be reset between retries
retryDelay Function function noDelay() { return 0; } 控制重试请求之间的延迟。默认情况下,重试之间没有延迟。另一个选项是exponentialDelay (Exponential Backoff). The function is passed retryCount and error.

测试

克隆这个仓库 然后 执行:

npm test

axios-retry

Axios plugin that intercepts failed requests and retries them whenever possible.

Installation

npm install axios-retry

Usage

// CommonJS
// const axiosRetry = require('axios-retry');

// ES6
import axiosRetry from 'axios-retry';

axiosRetry(axios, { retries: 3 });

axios.get('http://example.com/test') // The first request fails and the second returns 'ok'
  .then(result => {
    result.data; // 'ok'
  });

// Exponential back-off retry delay between requests
axiosRetry(axios, { retryDelay: axiosRetry.exponentialDelay });

// Custom retry delay
axiosRetry(axios, { retryDelay: (retryCount) => {
  return retryCount * 1000;
}});

// Works with custom axios instances
const client = axios.create({ baseURL: 'http://example.com' });
axiosRetry(client, { retries: 3 });

client.get('/test') // The first request fails and the second returns 'ok'
  .then(result => {
    result.data; // 'ok'
  });

// Allows request-specific configuration
client
  .get('/test', {
    'axios-retry': {
      retries: 0
    }
  })
  .catch(error => { // The first request fails
    error !== undefined
  });

Note: Unless shouldResetTimeout is set, the plugin interprets the request timeout as a global value, so it is not used for each retry but for the whole request lifecycle.

Options

Name Type Default Description
retries Number 3 The number of times to retry before failing. 1 = One retry after first failure
retryCondition Function isNetworkOrIdempotentRequestError A callback to further control if a request should be retried. By default, it retries if it is a network error or a 5xx error on an idempotent request (GET, HEAD, OPTIONS, PUT or DELETE).
shouldResetTimeout Boolean false Defines if the timeout should be reset between retries
retryDelay Function function noDelay() { return 0; } A callback to further control the delay in milliseconds between retried requests. By default there is no delay between retries. Another option is exponentialDelay (Exponential Backoff). The function is passed retryCount and error.
onRetry Function function onRetry(retryCount, error, requestConfig) { return; } A callback to notify when a retry is about to occur. Useful for tracing. By default nothing will occur. The function is passed retryCounterror, and requestConfig.

Testing

Clone the repository and execute:

npm test

 文章来源地址https://www.toymoban.com/news/detail-496158.html

到了这里,关于axios-retry插件-axios请求失败自动重试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Axios请求失败重刷接口

    页面接口请求时偶尔会出现 Network Error 异常报错,重新请求就会请求成功 接口没办法捕获异常原因,前端来做一次重刷解决问题 net::ERR_SSL_PROTOCOL_ERROR net::ERR_CONNECTION_REFUSED 记录请求map(以url为唯一标识),并设置单个接口重刷最大次数 格式:{ ‘https://xxxx/xxx?id=1’, 0 } 请求成

    2024年02月07日
    浏览(21)
  • 关于自动化测试用例失败重试的一些思考

    自动化测试用例失败重跑有助于提高自动化用例的稳定性,那我们来看一下,python和java生态里都有哪些具体做法? 如果是在python生态里,用pytest做测试驱动,那么可以通过pytest的插件pytest-rerunfailures来实现失败用例重跑,具体的使用方式有两种,一种是通过命令行指定pytes

    2024年02月14日
    浏览(28)
  • 重试框架入门:Spring-Retry&Guava-Retry

    在日常工作中,随着业务日渐庞大,不可避免的涉及到调用远程服务,但是远程服务的健壮性和网络稳定性都是不可控因素,因此,我们需要考虑合适的重试机制去处理这些问题,最基础的方式就是手动重试,侵入业务代码去处理,再高端一点的通过切面去处理,较为优雅的

    2024年02月13日
    浏览(34)
  • rabbitmq:retry重试机制和延迟消息的实现

    rabbitmq:retry重试机制和延迟消息的实现 在消费者消费消息的时候可能会因为网络等外部原因导致消息处理失败,这个时候如果将消息直接丢弃会导致正常的业务丢失,但是如果是一条本身就有问题的消息,那么这个时候又必须丢弃掉,如果选择用channel.basicNack 或 channel.basi

    2024年02月13日
    浏览(28)
  • Java 重试框架 Spring-Retry | 快速入门

    👉 本节目标:了解传统重试的写法以及 Spring-Retry 快速入门。 下面使用一个例子来讲述:调用第三方接口获取数据,支持重试 3 次,每次重试间隔 5 秒。 传统写法 :while 循环,判断是否有异常,有异常则重试,并使用 Thread 延迟,直到重试次数用完或重试成功为止。 调用第

    2024年04月10日
    浏览(31)
  • Spring-retry 优雅的实现循环重试功能

    引言      在实际的应用场景中,可能经常会遇到,当请求一个接口调一个服务的时候,出现异常或网络出现故障的情况下就会失败,而对于那些重要的服务当失败后,可能我们就会进行重试,多调用几次,如果还是失败再另外进行单独处理。接下来,就是要讲解的重点内容

    2024年02月12日
    浏览(34)
  • 基于 Guava Retry 在Spring封装一个重试功能

    pom依赖 注解类 测试类

    2024年02月13日
    浏览(28)
  • VUE项目使用axios发送post跨域请求,返回数据失败问题

    Access to XMLHttpRequest at \\\'http://xxxx\\\' from origin \\\'http://localhost:8080\\\' has been blocked by CORS policy: Response to preflight request doesn\\\'t pass access control check: No \\\'Access-Control-Allow-Origin\\\' header is present on the requested resource. 第一步 ,在后端接受方,对返回的数据添加 响应头 ,使用下面这句代码: 第二步

    2024年02月11日
    浏览(38)
  • 小程序支付报错:向微信请求统一下单失败:商户号该产品权限未开通,请前往商户平台>产品中心检查后重试

    一.检查微信商户和小程序是否建立绑定关系,没有绑定的需要进行绑定 1.登录微信商户平台,产品中心--AppId账号管理--关联AppId 2.填写要绑定的小程序AppId和认证主体点提交(可在微信公众平台--设置--基本设置获取) 3.登录微信公众平台 --功能--微信支付--待关联商户号--绑定  二

    2024年02月11日
    浏览(46)
  • 分布式重试服务平台 Easy-Retry

      在介绍这款开源产品前先给大家介绍一个开源组织:aizuda–爱组搭   可以看到Easy-Retry就是爱组搭的开源项目之一。   在分布式系统大行其道的当前,系统数据的准确性和正确性是重大的挑战,基于CAP理论,采用柔性事务,保障系统可用性以及数据的最终一致性成为

    2024年02月09日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包