Retry HTTP requests with Axios.


Use Axios interceptors to automatically retry failed requests. Super flexible. Built in exponential backoff.


npm install retry-axios


To use this library, import it alongside of axios:

// Just import rax and your favorite version of axios
const rax = require('retry-axios');
const axios = require('axios');

Or, if you're using TypeScript / es modules:

import * as rax from 'retry-axios';
import axios from 'axios';

You can attach to the global axios object, and retry 3 times by default:

const interceptorId = rax.attach();
const res = await axios('https://test.local');

Or you can create your own axios instance to make scoped requests:

const myAxiosInstance = axios.create();
myAxiosInstance.defaults.raxConfig = {
  instance: myAxiosInstance
const interceptorId = rax.attach(myAxiosInstance);
const res = await myAxiosInstance.get('https://test.local');

You have a lot of options...

const interceptorId = rax.attach();
const res = await axios({
  url: 'https://test.local',
  raxConfig: {
    // Retry 3 times on requests that return a response (500, etc) before giving up.  Defaults to 3.
    retry: 3,

    // Retry twice on errors that don't return a response (ENOTFOUND, ETIMEDOUT, etc).
    noResponseRetries: 2,

    // Milliseconds to delay at first.  Defaults to 100.
    retryDelay: 100,

    // HTTP methods to automatically retry.  Defaults to:
    // ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT']
    httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT'],

    // The response status codes to retry.  Supports a double
    // array with a list of ranges.  Defaults to:
    // [[100, 199], [429, 429], [500, 599]]
    statusCodesToRetry: [[100, 199], [429, 429], [500, 599]],

    // If you are using a non static instance of Axios you need
    // to pass that instance here (const ax = axios.create())
    instance: ax,

    // You can set the backoff type.
    // options are 'exponential' (default), 'static' or 'linear'
    backoffType: 'exponential',

    // You can detect when a retry is happening, and figure out how many
    // retry attempts have been made
    onRetryAttempt: err => {
      const cfg = rax.getConfig(err);
      console.log(`Retry attempt #${cfg.currentRetryAttempt}`);

If the logic in onRetryAttempt requires to be asynchronous, you can return a promise, then retry will be executed only after the promise is resolved:

const res = await axios({
  url: 'https://test.local',
  raxConfig: {
    onRetryAttempt: err => {
      return new Promise((resolve, reject) => {
        // call a custom asynchronous function
        refreshToken(err, function(token, error) {
          if (!error) {
            window.localStorage.setItem('token', token);
          } else {

Or if you want, you can just decide if it should retry or not:

const res = await axios({
  url: 'https://test.local',
  raxConfig: {
    // Override the decision making process on if you should retry
    shouldRetry: err => {
      const cfg = rax.getConfig(err);
      return true;

How it works

This library attaches an interceptor to an axios instance you pass to the API. This way you get to choose which version of axios you want to run, and you can compose many interceptors on the same request pipeline.






  • 项目整理11.27——axios相关

    1、配置请求的根路径 axios.defaults.baseURL = ''2、请求任意接口之前进行统一拦截判断 //请求任意...

    1 年前
  • 逐行解析Axios源码

    源码目录结构 # 📁 lib # |—— 📁 adapters // axios主要使用的请求方法 # |—— |—— 📃 http.js // axios中node端使用的请求函数...

    1 年前
  • 记录 关于axios封装

    废话不多说,直接上代码,本人也是菜鸡 如果有什么不对请指教~ ps: message为iview组件的插件 按需引入。 // 引入axios import axios from 'axios'; i...

    1 年前
  • 记一次eggjs+axios传输multipart的纠错过程

    总所周知,egg的csrf策略让post每次都要发送token码验证,为了方便,我在axios的interceptor里作了前置拦截。 结果不幸从此发生! 最开始没有看官方文档,以为csrf应该加在d...

    1 年前
  • 解决vue-axios和vue-resource跨域问题

    最近在学习vue,在vue中没有ajax,而是利用vue-resource和vue-axios进行数据通信。Vue2.0之后,官方推荐使用vue-axios。 问题 在使用vue-axios的po...

    3 年前
  • 表单按钮重复提交,axios重复请求的处理方案

    1.自定义指令directive思路: 点击之后当前按钮一定时间内(例如:setTimeout 1000ms)不可以再次被点击,相当于节流;但是如果一个请求1000ms以上还没有返回,那么再次点击就会...

    5 个月前
  • 聊一聊Axios与登录机制

    前言 因为HTTP是一个stateless的协议,服务器并不会保存任何关于状态数据。 所以需要登录功能让服务器在以后请求的过程中能够识别到你的身份,而不是每次发请求都要输入用户名和密码。

    6 个月前
  • 第八课时: vue中axios请求拦截封装

    1.解决跨域问题 出现跨域的情况(受浏览器同源策略的影响): 解决跨域的方法: 1) 前端项目增加跨域代理 在vue.config.js文件中添加 后端增加header 2、封...

    1 年前
  • 碎片化视频讲解vue axios的使用

    B站链接地址: 视频讲解vue axios如何使用 视频主要讲解如何在vue中使用axios,进行ajax请求。 ...

    2 年前
  • 用React+Antd封装Axios实现全局Loading效果

    前言 今天在做react后台管理的时候要实现一个全局Loading效果,通常使用axios库与后端进行数据交互。为了更好的用户体验,在每次请求前添加一个加载效果,让用户知道在等待加载。

    7 个月前


扫码加入 JavaScript 社区