title: node.js forEach not blocking
body: <p>I am trying to process data from a MySQL table by pulling 7 fields and creating another table with the table name matching field 1 and the other 6 fields are data put into the fields of newly created table.  Field 1 is an identifier for a device and I want to log any changes to the other information in the other 6 fields.  For Example...</p>
  
  <p>Table A</p>
  
  <p>Row 1 ---> 90:59:AF:4E:C3:30 | Mark | 10051 | PV320 | 192.168.1.199 | 1.35 | 026132956282</p>
  
  <p>Row 2 ---> 90:59:AF:55:A3:BA | Buds | 10050 | PV200 | 192.168.1.123 | 1.37 | 026132966540 </p>
  
  <p>I want to create </p>
  
  <p>Table 90:59:AF:4E:C3:30</p>
  
  <p>Row 1 ---> Mark | 10051 | PV320 | 192.168.1.199 | 1.35 | 026132956282 </p>
  
  <p>and..Table 90:59:AF:55:A3:BA</p>
  
  <p>Row 1 ---> Buds | 10050 | PV200 | 192.168.1.123 | 1.37 | 026132966540</p>
  
  <p>The problem I am running into, is that I have about 70 records, and when I try to run my code, I cannot get it to wait for the first table to be created and populated before creating and populating the next table.  I know my syntax is correct, because it will create and populate several tables before throwing 'Error: ER_CON_COUNT_ERROR: Too many connections'</p>
  
  <p>Here is my code: </p>
  
  <pre><code>function read_all_devices(){
  var connection = mysql.createConnection({
      host     : db_host, 
      user     : user, 
      password : pass, 
      database : my_db'
  });
  connection.query('SELECT macaddress, customer_name, ssh_port, validation_type, ip, version, serialnumber FROM ' + table + ' WHERE 1', function(err, rows) {
      if (err) {
          log(RED+err);
      }
      if (!err &amp;&amp; rows[0]) {
          rows.forEach(function(current){
              var schema = mysql.createConnection({
                  host    : db_host,
                  user    : user,
                  password: pass,
                  database: 'information_schema'
              }); 
              schema.query("SELECT * FROM information_schema.tables WHERE table_schema = my_db AND table_name = '"+current.macaddress+"';", function(error, result) { //Check to see if the table already exists
                  if (error){
                      log("ERROR: " + error);
                  }
                  else if (result[0]) { 
                      log("Table for " + current.macaddress + " already exists.");
                      update_historical_table(current.macaddress, current.customer_name, current.ssh_port, current.validation_type, current.ip, current.version, current.serialnumber);
                  }
                  else {
                      log("Table for " + current.macaddress + " did not exist....creating.");
                      create_table_by_mac(current.macaddress, current.customer_name, current.ssh_port, current.validation_type, current.ip, current.version, current.serialnumber, update_historical_table);
                  }
              });
          });
      }
      connection.end();
  });
  </code></pre>
  
  <p>}</p>
  
  <pre><code>function create_table_by_mac(mac_to_create, name, port, val_type, ip, version, sn, printer, callback) {
  var connection = mysql.createConnection({
      host     : db_host, 
      user     : user, 
      password : pass, 
      database : my_db
  });
  connection.query("CREATE TABLE `" + mac_to_create + "` (recnum INT(10) NOT NULL AUTO_INCREMENT, customer_name VARCHAR(30) default NULL, ssh_port VARCHAR(6) default NULL, serialnumber VARCHAR(20) default NULL, validation_type VARCHAR(20) default NULL, ip VARCHAR(20) default NULL, version VARCHAR(20) default NULL, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (recnum));", function (create_error, create_result) {
      if (create_error) {
          log("ERROR Creating Table: "+ create_error);
      } else {
          log("Table " + mac_to_create + " created");
      }
  });
  callback(mac_to_create, name, port, val_type, ip, version, sn);
  
  function update_historical_table(mac_to_update, name, port, val_type, ip, version, sn) {
  var connection = mysql.createConnection({
      host     : db_host, 
      user     : user, 
      password : pass, 
      database : my_db
  });
  connection.query("INSERT INTO `"+ mac_to_update + "` (`customer_name`, `ssh_port`, `validation_type`, `ip`, `version`, `serialnumber`, `printer_type`) VALUES ('" + name + "', '" + port + "', '" + val_type + "', '" + ip + "', '" + version + "', '" + sn + "');", function(err, rows) {
      connection.end();
      if (!err) {
          log(GREEN+"Table "+ WHITE + mac_to_update + GREEN + " updated successfully.");
      } else if (err) {
          log(RED+"ERROR Updating "+ WHITE + mac_to_update + ".");
          log(RED+"ERROR MESSAGE: " + err);
      }
  });
  </code></pre>
  
  <p>}</p>
  
  <p>This is the best I could get the code to paste.  If there are any questions as to what I am trying to do here, please let me know.</p>