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

203
Views
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 answers
Answer question

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 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!