Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

428
Visualizações
cancel axios request if it exceeds timeout and retry if the previous requests were timed out in nodejs

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda