axios.all + axios-retry 重试

axios.all 只要一个请求出现失败 , 就不会走then.

https://segmentfault.com/q/1010000022011292
在这里插入图片描述

使用 axios-retry 来使每个请求确保成功.

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

// https://segmentfault.com/q/1010000022011292
// https://stackoverflow.com/questions/56074531/how-to-retry-5xx-requests-using-axios


axiosRetry(axios, {
   retries   : 3, // number of retries
   retryDelay: (retryCount) => {
      console.log(`retry attempt: ${retryCount}`);
      return retryCount * 200; // time interval between retries
   }
   /*
   retryCondition: (error) => {
         // if retry condition is not specified, by default idempotent requests are retried
         return error.response.status === 500;
      },
   */
});


function test() {
   let p1 = new Promise((resolve, reject) => {
      setTimeout(() => {
         console.log('success, 1');
         resolve(1)
      }, 1000)
   });

   /*
      let p2 = new Promise((resolve, reject) => {
         setTimeout(() => {
            console.log('fail, 2')
            reject(2)
         }, 2000)
      })
   */
   let p2 = axios({
      method: 'GET',
      // url   : 'https://ar.jingaisheji.com/tour/c8b898dbaa8fb34a'
      url   : 'https://test.local'
   });

   let p3 = new Promise((resolve, reject) => {
      setTimeout(() => {
         console.log('success, 3');
         resolve(3)
      }, 3000)
   });

   axios.all([p1, p2, p3]).then(
      res => {
         console.log(res);
         console.log('all done')
      }, error => {

         console.log('someone failed')
      })
}

test();



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值