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

140
Views
how to wait until the function has completed running (for loop) in node

now the problem is the for loops just instantly loops everething but i want it to wait before looping, because evetime the loop runs it inserts data into the database (and that takes time) so i want it to wait until the data has been inserted and then increment the value of i and loop again

function insert_into_db(){
  for (let i = (howmuch/dueAmount);i>0;i--){

    clientChartArray = [aa1,bb1,(cc1*i),dd1,ee1,ff1,gg1,hh1] 
    PreviousChangesArray = [a1,b1,c1,d1,rowid]

    if(check_bal){  
      clientChart.run(`UPDATE hp2269 SET RD = ?, DL = ?, BA = ?, RA = ?  WHERE rowid = ? ;`,PreviousChangesArray,(err)=>{
        if(check_reminder == false){ 
          chartInsert(clientChartArray)
        }
      })
    }else{
      chartInsert (clientChartArray)
    }
    
  }  
}

The problems it creates

  1. some insertions take longer than others
  2. So the loop doesn't wait for them and goes on with the next insertion
  3. So the entries GET JUMBLED and don't get inserted in order
  4. The last inserted row is also used in a different function so it has to be accurate in order to get the desired result

i am actully really new to working with database so i dont know if i am doing any this wrong or if there is a fundumental problem in my code so please consider helping me out TQ

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

0

In for loop you can wait the insertion to be finished. Note this not work with loops that require a callback include forEach, map, filter, and reduce.

async function insert_into_db(){
          for (let i = (howmuch/dueAmount);i>0;i--){
        
            clientChartArray = [aa1,bb1,(cc1*i),dd1,ee1,ff1,gg1,hh1] 
            PreviousChangesArray = [a1,b1,c1,d1,rowid]
        
            if(check_bal){  
              let result = await clientChart.run(`UPDATE hp2269 SET RD = ?, DL = ?, BA = ?, RA = ?  WHERE rowid = ? ;`,PreviousChangesArray);
               if(check_reminder == false){ //or check result
                  await chartInsert(clientChartArray)
                  //do something
                }
            }else{
              await chartInsert (clientChartArray)
              // do something
            }
            
          }  
        }
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!