title: execute two db query in which one query result pass as input to another query asynchronous node js
body: <pre><code>  //global array to store dynamodb query result 
    var store = [];
  
    //function that  to retrive socket data based on user email id as a hash key and give json to pass into query by taking email as input
    var getSocketParams = (email) =&gt; {
      return {
          TableName: "socketSockets",
          KeyConditionExpression: "user_id = :em",
          ExpressionAttributeValues: {
              ":em": email
          }
      };
   }
   //query 1 which retrive all sockets details from respective table 
   var q1 = (callback) =&gt; {
       retrivalDetail(getSocketParams(em), function(err, res) {
           if (err) callback(err, null);
           callback(null, res);
       });
   };
  
   //query 2 take input as a result of query 1 response and second a callback 
   var q2 = (res, callback) =&gt; {
       //here i made some logic to check callback is not executed until all items in query 1 response executed and that respective data fetched from respective table if have any 
       var ctr = 0;
       res.Items.forEach(function(d) {
           var payload_params_l = {
               TableName: "socketPayload",
               KeyConditionExpression: "#mac = :mac",
               ExpressionAttributeNames: {
                   "#mac": "mac"
               },
               ExpressionAttributeValues: {
                   ":mac": d.socket_id
               },
               ScanIndexForward: false,
               Limit: 1
           };
           retrivalDetail(payload_params_l, function(err, res) {
               if (err) callback(err, null);
               store.push(res);
               ctr++;                                
               if (ctr === Object.keys(res.Items).length) {
                   callback(null, store);
               }
           });
       });
   };
   //my function call to excute query 1 and query 2 here problem is due to asynchronous nature of node js i guess  before getting query 1 result completely query2 will be start executing so i place q2 inside q1 callback here how can i retrive all data from q2 inside loop and pass all data after finishing loop to q2 callback 
   function call() {
       q1(function(err, res) {
           if (err) done(err, null);
           q2(res, function(err, res) {
               if (err) done(err, null);
               //final function done which gives response to API request done(error ,response)
               done(null, res);
           });
       });
   };
   call();
  </code></pre>
  
  <p>Here the issue i am facing is  due to asynchronous nature of event loop of node js i guess before getting query 1 result completely query2 will be start executing so i place q2 inside q1 callback here i want better suggestion to improve this code that how can i retrive all data from q2 inside loop and pass all data after finishing loop to q2 callback </p>
  
  <p>i am newbie to use async package in node js if possible using async module how it would be any help regarding this</p>