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

228
Visualizações
JavaScript Try catch how to call same function in catch to retry

In JavaScript Try catch block how to retry the same . am facing once scenario any one ca give the best approach to handle this ?

for Example :

const getMyDetails = async()=>{
try{
 await getName();
}catch(err){
 //But Above FAIL due to some issue
 // so I want to try the same again "getName" here until the success - 'not single time'
}

// Second Method for get data 
const getName = async()=>{
try{
 here am calling API or any kind of subscription But here its failing so am throwing error 
}catch(err){
 throw new Error(err);
}

getMyDetails()

Note: may be the fail reason like Databse events or some other subscriptions etc..

instead of calling the method name in catch .. what will be the best approach for retry

Thanks in advance

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

0

Just Calling Itself If It Fails Will Do The Job


const getMyDetails = async()=>{
try{
 await getName();
}catch(err){
 getMyDetails()
}

// Second Method for get data 
const getName = async()=>{
try{
  // Method
}catch(err){
 throw new Error(err);
}

getMyDetails()

about 4 years ago · Juan Pablo Isaza Relatório

0

Here you can see an example behavior and the implementation logic of calling an async function N number of times, only until no errors are thrown.

// We must be inside of an async function to be able to call await
(async function() {

  // Example async function which returns the value only if no errors are thrown,
  // otherwise calls itself to retry the async call
  async function getMyDetails() {
    try {
      return await getName();
    } catch (error) {
      return getMyDetails();
    }
  }

  // Example async function which returns value only if the random number is even,
  // otherwise throws an Error (simulating an API error response)
  async function getName() {
    return new Promise((resolve, reject) => {
      let randomNumber = Math.floor(Math.random());
      if (randomNumber % 2 === 0) {
        resolve("Random Name");
      }
      reject("Example API Error has occured.")
    });
  }

  let myDetails = await getMyDetails();
  console.log(myDetails);
  
})();

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