title: How to fetch data from database inside the for loop in node js
body: <p>fetching data inside the for loop using pool query in node js output values are correct but not at the correct position.
  Here is my code.</p>
  
  <pre><code>for(var k=0; k &lt; mdmid_array_implode_array_st.length; k++){
  var mdmid_arr=mdmid_array_implode_array_st[k];
  pool.query("select sum(cnt_deal_amt) as cnt_deal_amt from crm_clients_feedback_log where cnt_mdm_id IN ("+mdmid_arr+") ORDER BY cnt_mdm_id ASC",function(dealerr,dealrslt){
  dealrslt_sa=dealrslt[0].cnt_deal_amt;
  console.log(dealrslt);
  })
  }
  
  OUtPUT:
  [ RowDataPacket { cnt_deal_amt: 512529 } ]
  [ RowDataPacket { cnt_deal_amt: 33000 } ]
  [ RowDataPacket { cnt_deal_amt: null } ]
  [ RowDataPacket { cnt_deal_amt: 1000 } ]
  [ RowDataPacket { cnt_deal_amt: null } ]
  [ RowDataPacket { cnt_deal_amt: 43600 } ]
  [ RowDataPacket { cnt_deal_amt: 0 } ]
  [ RowDataPacket { cnt_deal_amt: 0 } ]
  [ RowDataPacket { cnt_deal_amt: 9500 } ]
  [ RowDataPacket { cnt_deal_amt: null } ]
  [ RowDataPacket { cnt_deal_amt: null } ]
  [ RowDataPacket { cnt_deal_amt: 11500 } ]
  </code></pre>
  
  <p>output result values are not at correct position also every time position changed when refreshing the page.</p>
  
  <pre><code>[ RowDataPacket { cnt_deal_amt: null } ]
  [ RowDataPacket { cnt_deal_amt: 512529 } ]
  [ RowDataPacket { cnt_deal_amt: null } ]
  [ RowDataPacket { cnt_deal_amt: 43600 } ]
  [ RowDataPacket { cnt_deal_amt: 0 } ]
  [ RowDataPacket { cnt_deal_amt: 9500 } ]
  [ RowDataPacket { cnt_deal_amt: 33000 } ]
  [ RowDataPacket { cnt_deal_amt: null } ]
  [ RowDataPacket { cnt_deal_amt: 0 } ]
  [ RowDataPacket { cnt_deal_amt: 1000 } ]
  [ RowDataPacket { cnt_deal_amt: 11500 } ]
  [ RowDataPacket { cnt_deal_amt: null } ]
  </code></pre>