title: Wait for current loop in for loop to finish, before continuing
body: <p>What is the best way to make javascript wait for the current iteration of the for loop to finish before the next one?</p>
  
  <p>Heres my problem.</p>
  
  <p>At the moment, I am running an SQL select statement from a SQLite database for each item in a for loop, like this.</p>
  
  <pre><code>for(let i = 0; i &lt; 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";
      }
  }
  </code></pre>
  
  <p>My issue is that as it runs through the loop, it starts to run the insert statement before the select is complete.</p>
  
  <p>For example</p>
  
  <blockquote>
    <p>//First iteration of loop</p>
    
    <p>"SELECT * FROM table WHERE id = 1";</p>
    
    <p>"INSERT INTO table (id) VALUES (1)";</p>
    
    <p>//Second iteration of loop</p>
    
    <p>"SELECT * FROM table WHERE id = 2";</p>
    
    <p>"INSERT INTO table (id) VALUES (1)";</p>
  </blockquote>
  
  <p>It is doing this because its not waiting for the current iteration of the for loop to finish, before starting the next.</p>
  
  <p>How do I make it so that the for loop waits for the current loop to finish, before starting a new one?</p>