title: Using Promises inside nested for loops in node.js
body: <p>I am still in learning stage of Node.js and I have a scenario where in</p>
  
  <p>I have 3 tables in mysql <strong>category</strong>, <strong>subcategory</strong> and <strong>items</strong> which has name and id for all rows.</p>
  
  <p>Now,I have have list of folders and some .xlsx files in every folder and some sheets in every .xlsx file</p>
  
  <p>Where the foldername is the categoryname in the category tables</p>
  
  <p>.xlsx filename is the subcategoryname in the subcategory table and
  the sheet name inside every .xlsx file is the itemname in the items table</p>
  
  <p>What I want to do is I need to loop through every folder,file and sheet and crete a mapping table of there Id's .</p>
  
  <p>my code so far : </p>
  
  <pre><code>const fs = require('fs');
  const path = require('path');
  const xlsx = require('node-xlsx').default;
  const mysql = require('mysql');
  const connection = require('./db-connection.js');
  
  var categoryMapping = () =&gt; {
  
  //directory of all files
  var mainDirectory = `${__dirname}/xlsx/final_data`;
  
  //reading mainDirectory
  fs.readdir(mainDirectory, function(err, files) {
  
      if (err) {
          console.error("Could not list the directory.", err);
          process.exit(1);
      }
  
      files.forEach(function(file, index) {
  
          //taking the folder name and getting the id of that name
          var sql = 'SELECT xxx FROM xxx WHERE xxx = ' + mysql.escape(file);
  
          connection.query(sql, function(error, result) {
              if (error) {
                  console.log(error);
              }
  
              if (result.length &gt; 0) {
                  console.log('cat id:', result[0].CategoryId);
              } else {
                  //fs.appendFile('category.log',`${file} not present in xxx table` + '\n');
                  console.log(`${file} not present in xxx table`);
              }
          });
  
          //directory of .xlxs files
          var subCategoryDirectory = path.join(mainDirectory, file);
  
          fs.readdir(subCategoryDirectory, function(err, sheets) {
  
              if (err) {
                  console.error("Could not list the directory.", err);
                  process.exit(1);
              }
  
              sheets.forEach(function(sheet, index) {
  
                  //taking the file name and getting the id of that name
                  var filename = path.parse(sheet).name;
  
                  var sql = 'SELECT xxx FROM xxx WHERE xxx = ' + mysql.escape(filename);
  
                  connection.query(sql, function(error, result) {
                      if (error) {
                          console.log(error);
                      }
                      if (result.length &gt; 0) {
                          console.log('sub cat id:', result[0].SubCategoryId);
                      } else {
                          //fs.appendFile('sub-category.log', `${filename} in ${subCategoryDirectory} not present in xxx table` + '\n');
                          console.log(`${filename} not present in xxx table`);
                      }
                  });
  
                  //individual .xlxs file path
                  var sheetpath = path.join(subCategoryDirectory, sheet);
  
                  const fileData = xlsx.parse(sheetpath);
  
                  fileData.forEach(function(page, index) {
  
                      //taking the sheet name and getting the id of that name
                      var itemName = page.name;
                      var sql = 'SELECT SaltId FROM master_ddb_salts WHERE SaltName = ' + mysql.escape(itemName);
  
                      connection.query(sql, function(error, result) {
                          if (error) {
                              console.log(error);
                          }
                          if (result.length &gt; 0) {
                              console.log('item id:', JSON.stringify(result[0].ItemId));
                          } else {
                              //fs.appendFile('salts.log', `${itemName} of ${sheetpath} not present in salt-master table` + '\n');
                              console.log(`${itemName} not present in xxx table`);
                          }
                      });
  
                      //maybe here I need to enter all categoryId, subCategoryId and itemId to mapping table 
  
                  });
              });
          });
      });
  });
  };
  
  categoryMapping();
  </code></pre>
  
  <p>My code so far is giving me all the ID's properly.</p>
  
  <p>I know the basic usage of <strong>Promises</strong> but I am not getting how to implement it here.</p>
  
  <p>Could someone explain how can I make use of Promises or Callbacks here?
  Any better solutions like making use of npm pakages like Q,bluebird or async are aslo OK.</p>