I have tried different ways to find the answer. If I use cancelToken library in axios(npm package). Once the request is cancelled, we can't retry the request. The request won't go to the next line(even after timeout) until it receives a response from the axios request.
I need an answer to cancel a request after timeout and should also retry the request incase of timeout.
Axios has an option "timeout", after that threshold it will cancel the request. So you can write something like this:
const TIMEOUT = 3000; // millisecods
let succeededResponse = null;
while(!succeededResponse) {
try {
succeededResponse = await axios.get("https://somewhere.any", { timeout: TIMEOUT });
} catch(e) {
// as axios will throw an error in case of timeout, you'll need this try-catch block
}
}
// axios will request until it gets the response in the specified threshold
// here succeededResponse will have a value