如何使用axios-retry对状态200重试

时间:2019-03-19 18:58:08

标签: javascript axios

我正在使用axios-retry 我正在尝试按响应条件而不是响应状态重试。 我的状态是200,我认为是因为它不会重试, 我的代码是

        raxConfig: {
                retry: retry,
                retryDelay: retryDelay,
                httpMethodsToRetry: ["GET"],
                 statusCodesToRetry: [
                     [200, 300]

                 ],
                shouldRetry: (err) => {
                    const cfg = rax.getConfig(err);
                    if (cfg.data.isFinal == true) {
                        return false
                    } else {
                        return true;
                    }
                }

1 个答案:

答案 0 :(得分:0)

static async event( retry = 5, retryDelay = 10000) {
    //default retryDelay 10 sec
    const restURL ='www.sdfdsfds.sdfds'
    for (let i = 0; i < retry; i++) {
      const response = await this.rest(restURL);
      if (
        response.status == 200 

      ) {
        return response.data;
      }
      await this.delay(retryDelay);
    }
    return Promise.reject(new Error(400));
  }



static delay(milisec = 10000) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve("resolved");
      }, milisec);
    });

}