Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda