Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
JavaScript Try catch cómo llamar a la misma función en catch para reintentar

En JavaScript Try catch block cómo volver a intentar lo mismo. ¿Estoy enfrentando un escenario único que pueda dar el mejor enfoque para manejar esto?

por ejemplo :

 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()

Nota: puede ser el motivo del error, como eventos de la base de datos o algunas otras suscripciones, etc.

en lugar de llamar al nombre del método en catch... cuál será el mejor enfoque para reintentar

Gracias por adelantado

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Solo llamándose a sí mismo si falla hará el trabajo

 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 Report

0

Aquí puede ver un comportamiento de ejemplo y la lógica de implementación de llamar a una función asíncrona N número de veces, solo hasta que no se produzcan errores.

 // 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!