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

123
Views
while loop not waiting for await

I know there's a ton of these questions but I haven't been able to find one that fits my case. I'm making api calls in a while loop and just don't want to time out my calls by rapid firing them. The while loop doesn't seem to be waiting for the call to finish.

while(it < array.length){
    await apiFunction(array[it]);
    console.log(it);
    it++
}

and the function

async apiFunction(array){
    try{
        (async () => {
            const result = await api.doStuff(array);
            console.dir(result);
        }
    } catch (e) {
        stuff;
    }
}

I've also tried adding a return(result) but that didn't change anything

My console output is

0
1
result
result

How can I get the while loop to wait until the api call is done? so my console looks like

0
result
1
result
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I had too many layers of asynchronicity going I guess. Just removed the async from inside the apiFunction

async apiFunction(array){
    try{
        const result = await api.doStuff(array);
        console.dir(result);
    } catch (e) {
        stuff;
    }
}
about 4 years ago · Santiago Trujillo 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!