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

266
Visualizações
Error handling: How can I await and re execute function after a timeout

I would like to handle async timeouts by awaiting the execution and re-executing a function including error handling after it fails.

Pseudo code:

01 call and await async function update()
02 if no error -> continue to line 05
03 sleep for 10000ms
04 Jump to line 01 //call update() again and handle possible errors
05 //continues execution

seems simple, but I cant get the asynchronous part working

Some common structure without any promise handling


let success = false;

while (!success) {
  try{
    update()
    success = true;
  } catch (e) {
    setTimeout(() => {},10000)
  }
}

I know that I should be working with .catch and promises, but cant figure out how.

Thanks for any help and explanations

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

0

Not sure if this is what you're looking for, but here's an example of retrying an async call until success, with a 3 second delay between each attempt:

const wait = timeout => new Promise(resolve => setTimeout(resolve, timeout));

let counter = 0;

async function update() {
  console.log('Updating...');
  await wait(1000)
  if (counter === 3) {
    console.log('Update success');
  } else {
    counter += 1;
    throw new Error('Update failure');
  }
}

async function callUpdateUntilSuccess() {
  try {
    await update();
  }catch(err) {
    console.error(err);
    await wait(3000);
    return callUpdateUntilSuccess();
  }
}

(async () => {
  await callUpdateUntilSuccess();
})();

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