asounrb
asounrb

Reputation: 29

Can I make a new get/post request upon timeout error using axios?

Having tried to pull data using get/post request using axios, I have faced some timeout errors. Could anyone let me know if there is any way I can make another get/post request in catch block?

I'm using nodejs only to do somethings for my back-end. (not using React or anything else)

Say, what I would like to implement is something like the below.

try {
    let data = await axios.get(url) 
    return data
} catch(e) {
    if (e.errortype == 'timeout') 
    make another await axios.get(url) and repeat this till it gives an appropriate response 
}

Upvotes: 0

Views: 83

Answers (1)

jonhendrix
jonhendrix

Reputation: 366

Couldn't you just create your request within a function and call it again on failure?

const request = async () => {
  try {
    const url = "https://someurl.com/";
    let data = await axios.get(url);
    return data;
  } catch (e) {
    if (e.errortype == "timeout") {
      request();
    }
  }
};

Upvotes: 1

Related Questions