【问题标题】:Timed retry on axios failureaxios失败的定时重试
【发布时间】:2019-01-24 03:33:52
【问题描述】:

我正在为私人使用而开发的节点模块中使用 axios。在网络故障时,我想以特定的时间间隔重试(如指数退避)。我在我的另一个 react-redux 项目中使用 redux-saga 的延迟完成了此操作。下面是它的sn-p:

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

我想使用普通的 es6 实现相同的目标,或者如果有任何轻量级延迟库,请建议我。

【问题讨论】:

    标签: javascript ecmascript-6 promise axios


    【解决方案1】:

    您可以使用 Bluebird 并使用其额外的 Promise 功能也可以使用 Promise.delay

    这将使您获得与您发布的内容类似的内容:

    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") ;
    });
    

    【讨论】:

      【解决方案2】:

      我不想仅出于此目的使用第三方库。我创建了一个自定义延迟方法,该方法返回承诺并在特定时间点后解决。这是相同的sn-p:

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

      这解决了我的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-17
        • 2013-11-14
        • 2016-05-27
        • 1970-01-01
        • 2019-11-16
        • 1970-01-01
        • 2020-11-26
        • 1970-01-01
        相关资源
        最近更新 更多