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

200
Views
How to do many SQL queries in a for loop with Nodejs and Express

I have a req.body from a Post form with many values like this

console.log(req.body)

{

productid:[1, 2, 3]
qty: [3, 5, 6]

}

I want to make a for loop and use the different values in a SQL query But I get stuck on the first query problaby because its an async await function.

My question is how can I do many sql queries with a forloop and still use an async await function?

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

0

You can do it like this:

for(let item of array) {
   let res = await query(item)
}
about 4 years ago · Juan Pablo Isaza Report

0

Since I only do an update and not waiting for an answer I didnt need to use await async. It worked when I remove async/await.

Thank you

Sorry for not so clear question

about 4 years ago · Juan Pablo Isaza Report

0

if you want to do them in parallel you can create an array of promises and use Promise.all to wait for all the operations to finish, in this way you can optimize you code a lot instead of doing in a sequential way.

    const operations = []
    for(let item of array) {
      operations.push(query(item))
    }
    const result = await Promise.all(operations)
    //Do wherever you want with your results.

In order to execute your code, your function which handle the request must be declared with the async keyword.

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!