简体   繁体   English

定时重试axios故障

[英]Timed retry on axios failure

I am using axios in the node module that I am developing for private use. 我正在为私人开发的节点模块中使用axios。 On network failure, I want to perform retries in certain intervals (like exponential backoff). 在发生网络故障时,我想以一定的时间间隔执行重试(例如指数补偿)。 I have done this in my other react-redux project using delay from redux-saga. 我已经在我的其他react-redux项目中使用了redux-saga的延迟来完成此操作。 Below is the snippet of it: 以下是它的摘录:

return dealy(500).then(() => axios(error.config))

I want to achieve the same thing using plain es6 or suggest me if there are any light-weight delay libraries. 我想使用普通es6来实现相同的目的,或者建议我是否有任何轻量级的延迟库。

You could use Bluebird and get on top of its extra Promise features also Promise.delay . 您可以使用Bluebird并获得Promise.delay的其他 Promise功能。

This would allow you something similar to what you posted: 这将使您类似于发布的内容:

Promise.delay(500).then(function() {
    console.log("500 ms passed");
    return "Hello world";
}).delay(500).then(function(helloWorldString) {
    console.log(helloWorldString);
    console.log("another 500 ms passed") ;
});

I do not want to use third-party library for this only purpose. 我不想仅将第三方库用于此目的。 I had created a custom delay method that returns promise and resolves after certain point of time. 我创建了一个自定义的延迟方法,该方法返回promise,并在特定时间点后解析。 Here is the snippet for the same: 这是相同的代码段:

const delay = (timeMs) => new Promise((resolve) => setTimeout(() => resolve(), timeMs));

This solved my issue. 这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM