Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda