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

67
Views
Chaining Async Await with loop

I have 2 functions with loops that I want to execute sequentially:

  • Function A executes

  • Within Function A there is a For loop triggering an external function

  • Once loop in Function A is finished doing its thing, then we start with Function B

I have tried with the following code but function B starts in parallel, doesn't wait for Function A to finish:

async function sendPosts() {
    console.log("start send posts")
    for (let post of posts) {
        ...
  
        await channel.send({
            ...
        }).then((message) => {
            externalFunction(message);
        })
        .catch(err => console.log(err));
    }
    console.log("finish send posts")    
}

async function sendComments() {
    console.log("start send comments")
    for (let comment of commentsData) {
        ... 
    }
    console.log("finish send comments")
}   

sendPosts().then(sendComments());
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For of loop is not awaited. you looking for something as following

    for await (const variable of iterable) 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of

Foreach loop has acting the same

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!