title: Nodejs loops iterating before executing the mysql queries embedded inside them
body: <p><strong>I am unable to return the projectIds array after the mysql statements have finished executing as the loop is iterating before executing them. This results in empty array being returned everytime.</strong></p>
  
  <pre><code>function fetchProjects (projects, userId, callback) {
    projectIds = [];
      Object.keys(projects).forEach(function(key) {
        var row = projects[key];
        const sql = 'create table if not exists users_' + row.project_id + '(sno int primary key auto_increment, iteration_no int not null, user_id int not null, foreign key(iteration_no) references pro_' + row.project_id + '(iteration_no), foreign key(user_id) references user(user_id))';
        connection.query(sql, function(err, result) {
          if(err) {
            console.log(err.sqlMessage);
            return callback(err.sqlMessage, false);
          } else {
            // console.log("First statement executed");
            const sql1 = "select distinct t1.project_id, t1.project_name, t1.client, t1.initial_department_id, t1.start_date, t1.status, t1.current_department, t1.currently_assigned_user from project t1, users_" + row.project_id + " t2 where t1.project_id = '" + row.project_id + "' and (select user_id from users_" + row.project_id +" where user_id = " + userId + ")";
            connection.query(sql1, function(err, result1) {
              if(err) {
                console.log(err.sqlMessage);
                return callback(err.sqlMessage, false);
              } else {
                if(result1.length &gt; 0) {
                  console.log(result1);
                  projectIds.push(result1);
                }
              }
            });
          }
        });
        // console.log("Iterating");
      });
      return callback(false, projectIds);
  }
  </code></pre>