title: node.js, express - executing mysql queries one after another within loops in a synchronous way
body: <p>In my node.js, express app, I am making an ajax call with the <a href="https://github.com/visionmedia/superagent" rel="noreferrer">superagent</a> middleware. The call fetches database data in a complex  array using the <a href="https://github.com/felixge/node-mysql" rel="noreferrer">node-mysql</a> middleware through quite a few database queries. </p>
  
  <p>Before pasting the code, I am trying to explain in words what I am trying to do although the code would suffice to say what it wants to do with the addition that <strong><em>all the asynchronous things inside the first callback should be done in the synchronous way.</em></strong></p>
  
  <p><strong>Explanation:</strong></p>
  
  <p>Inside the callback of the first query , a for loop is executed to run the second query multiple times and after each loop, the next loop is to be called only after the callback of the second query is complete. Things are same for the next code lines as well.</p>
  
  <p><strong>Code:</strong></p>
  
  <p>You can <strong>however skip</strong> the innards( marked in comments) of the for loops to make things brief and easy if you want.</p>
  
  <pre><code>conn.query("SELECT * FROM `super_cats`",function(error, results, fields) {   
  
          if(error){console.log("erro while fetching products for homepage "+ error);}
  
          for(var i in results) { // FIRST FOR LOOP INSIDE THE FIRST QUERY CALLBACK
  
              /*Innards of for loop starts*/    
              var elem = new Object();
              var supcat_id=results[i].id;
              elem.super_id =supcat_id;
              elem.cats=new Array();
              var cat= '';
              /*Innards of for loop ends*/ 
  
              conn.query("SELECT * FROM `categories` WHERE `supcat_id`="+supcat_id,function(error_cats, results_cats, fields_cats) {   
  
                  if (error_cats) {console.log("erro while fetching cats for menu " + error_cats);}
  
                  for(var j in results_cats) {
  
                      /*Innards of for loop starts*/    
                      cat= new Object();
                      var cat_id=results_cats[j].id;
                      cat.cat_id=cat_id;
                      cat.cat_name=results_cats[j].cat_name;
                      cat.subcats=new Array();    
                      /*Innards of for loop starts*/    
  
                      conn.query("SELECT * FROM `subcategories` WHERE `category`="+cat_id,function(error_subcats, results_subcats, fields_subcats) { 
                          if (error_subcats) {console.log("erro while fetching subcats for menu " + error_subcats);}   
                          for(var k in results_subcats ){
  
                              /*Innards of for loop starts*/    
                              var subcat=new Object();
                              var subcat_id=results_subcats[k].id;
                              subcat.subcat_id=subcat_id;
                              subcat.subcat_name=results_subcats[k].subcategory;
                              cat.subcats.push(subcat);
                              elem.cats.push(cat);    
                              /*Innards of for loop starts*/
  
                          }// end of for loop for  results_subcats   
  
                      });
  
                  }// end of for loop for result_cats
              });   
  
              super_cats.push(elem);    
           }// end of for supercat results   
  
      res.send(super_cats)    
  });
  </code></pre>
  
  <p>I tried with the <a href="https://github.com/caolan/async" rel="noreferrer">async</a> middleware but in vain as I just could not figure out which function to use in this case .</p>
  
  <p>To be brief, requirements are :</p>
  
  <p>1) All the asynchronous things inside the first callback should be done in the synchronous way.</p>
  
  <p>2) the response should be sent to the ajax call only after all the calculations are done and not before that (as it would probably happen if things were asynchronous as they are in the existing code, <strong>wouldn't it ?</strong>)</p>