简体   繁体   中英

How to do retry on status 200 using axios-retry

I am using axios-retry I am trying that it will do retry on response condition and not on response status. My status is 200 I think because of that it not going to retry, My code is

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

}

I tried to throw a 500 with an axios interceptor and then I configured an axios-retry... but that turned ugly real quick...

I ended up doing this

if (retry) {
    const MAX = 5;
    let tried = 0;
    do {
        response = await axios.get(url, { ...options });
        console.warn('try# ', tried++, response.data);
        await delay(1000);
    } while (response.data.ok !== 1 && tried < MAX);
} else {
    response = await axios.get(url, { ...options });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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