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
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
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
}
}
}