Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
How to make a function wait until its recursive instances are completed too? - Javascript

I have two functions in JS, one calls another, but the first one tries again itself after one second if a condition goes false. The problem is, I want the second function to await until this recursive instances are done too.

Here's what I have until now:

async function firstFunction(){
  if(SomeCondition){
    //do something
  }
  else{
    //do something
    setTimeout(() => {
      firstFunction();
    }, 1000);
    //Tries again in 1 second
  }
}

async function secondFunction(){
  await firstFunction() //waiting firstFunction and its instances to finish 
}

Can't figure out how to solve this, any idea?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since you've made firstFunction async - use Promises to do what you need

async function firstFunction() {
  if (SomeCondition) {
    //do something
  } else {
    //do something
    await new Promise(r => setTimeout(r, 1000))
    await firstFunction();
  }
}

async function secondFunction() {
  await firstFunction() //waiting firstFunction and its instances to finish 
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda