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

135
Views
Async/await with multiple for loops

I am having a hard time wrapping my head around how aysnc/await works for my specific scenario. I have some for loops that I want to run parallel of each other but I also have other for loops that are dependent on the data create from one of the parallel for loops.

Loop 1 {
/* Delete some rows from a SQL Table */
}

Loop 2 {
/* Delete some rows from a SQL Table */
}

Loop 3 {
/* Add some rows to a SQL Table */
}

Loop 4 {
/* Update some rows from a SQL Table */
}

What I want to happen is Loop 1, Loop 2, and Loop 3 run in parallel instead of sequentially. I don't want Loop 4 to run until Loop 3 has finished. How could this be achieved with async/await. I am trying to improve runtime of this code but I need to use for loops to iterate through data in the database.

Alternatively, if there is a way to reduce the time complexity using a different method instead of for loops I am open to suggestions.

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

0

await Promise.all(promiseLoop1, promiseLoop2, promiseLoop3);

await promiseLoop4;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Edit:

In addition, you can build the SQL command string using loops but only call SQL once.

You may also want to look into SQL Transactions:

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/transactions-transact-sql?view=sql-server-ver15

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!