title: Loop through MySQL rows and store results in array
body: <p>I am trying to store details of affectedRows from a MySQL INSERT query using NodeJS. My mind is melting trying to comprehend callbacks and Promises. Being a single-man dev team I wanted to reach out and ask for the clearest explanation as to how a callback can be applied here in a foreach loop.</p>
  
  <p>The goal should be clear from these few lines of code; store data in the affected_rows[] array.</p>
  
  <pre><code>var affected_rows = [];
  
  asset_array.forEach(function(asset) { // Populate the asset table
      var query_string = "INSERT IGNORE INTO " + asset_table + " SET symbol = '" + asset[0] + "', name = '" + asset[1] + "'";
      connection.query(query_string, function(err, rows, fields) {
          if (err) throw err;
          if ( rows.affectedRows &gt; 0 ) {
              data_to_push = [asset_table, asset[0], asset[1]];
              affected_rows.push(data_to_push);
          }
      });
  });
  
  console.log(affected_rows); // [] for obvious async reasons
  </code></pre>