What is the best way to make javascript wait for the current iteration of the for loop to finish before the next one?
Heres my problem.
At the moment, I am running an SQL select statement from a SQLite database for each item in a for loop, like this.
for(let i = 0; i < myresult.length; i++){
var query = "SELECT * FROM table WHERE id = " + myresult[i].id;
var result = //Run sql here
if(result.length == 0){ // if result doesn't exist, make it.
var insertQuery = "INSERT INTO table";
}
}
My issue is that as it runs through the loop, it starts to run the insert statement before the select is complete.
For example
//First iteration of loop
"SELECT * FROM table WHERE id = 1";
"INSERT INTO table (id) VALUES (1)";
//Second iteration of loop
"SELECT * FROM table WHERE id = 2";
"INSERT INTO table (id) VALUES (1)";
It is doing this because its not waiting for the current iteration of the for loop to finish, before starting the next.
How do I make it so that the for loop waits for the current loop to finish, before starting a new one?