title: How to execute two mysql queries using promise in nodejs?
body: <p>Here is what i going to achieve, i want to have an JSON data that returned from my node.js server is joined based on the value of first mysql queries (array JSON data)</p>
  
  <p>if i just want to execute two mysql queries i just enable <em>multipleStatements: true</em> then the code will be like this :</p>
  
  <p>app.post('/product', function (req, res) {</p>
  
  <pre><code>connection.query('call getProductList; call rowCountTotal', function (err, rows, fields) {
      if (!err) {
          var response = [];
  
          if (rows.length != 0) {
              response.push({ 'result': 'success', 'data': rows });
          } else {
              response.push({ 'result': 'error', 'msg': 'No Results Found' });
          }
  
          res.setHeader('Content-Type', 'application/json');
          res.status(200).send(JSON.stringify(response));
      } else {
          res.status(400).send(err);
      }
  });
  </code></pre>
  
  <p>});</p>
  
  <p>than the data will showed up two JSON that are separated in two arrays, but what i want to build here is one JSON with multiple array JSON data, which is looked like this :</p>
  
  <p>Sample JSON that i want :</p>
  
  <pre><code>[  
     {  
        "product_id":"1",
        "product_name":"MX-001",
        "product_attachment":[  
           {  
              "product_id":"1",
              "product_attachment_id":"1",
              "file_name":"maxgrand5.jpg",
              "file_path":"assets"
           }
        ]
     }
  ]
  </code></pre>
  
  <p>And here is what i trying to do in my node.js server side code, i trying to use </p>
  
  <pre><code>Promise.all (i think this code i should use right?) :
  
      return new Promise(function(resolve, reject) {
  
          Promise.all(connection.query('call getProductSingle("'+ product_series +'")', function (err, rows, fields) {
              if (!err) {
                  var response = [];
  
                  if (rows.length != 0) {
                      response.push({ 'result': 'success', 'data': rows });
                  } else {
                      response.push({ 'result': 'error', 'msg': 'No Results Found' });
                  }
  
                  connection.query('call getProductAttachment("'+ rows[0][0].product_id +'")', function (err, rowsAttachment, fields) {
                      if (!err) {
                          console.log("second query");
                          if (rowsAttachment.length != 0) {
                              response.push({'product_attachment': rowsAttachment });
                          } else {
                              response.push({ 'result': 'error', 'msg': 'No Results Found' });
                          }
                      }
                  });
                  console.log("outside second query");
                  res.setHeader('Content-Type', 'application/json');
                  res.status(200).send(JSON.stringify(response));
              } else {
                  res.status(400).send(err);
              }
  
              console.log("last");
              if (err) {
                  return reject(err);
              }
              resolve(res);
          }));
      });
  </code></pre>
  
  <p>here is my Stored Procedure result which named in 'getProductSingle' :</p>
  
  <ol>
  <li>product_id = 1</li>
  <li>product_name = MX-001</li>
  </ol>
  
  <p>and here is my second procedure result 'getProductAttachment' :</p>
  
  <ol>
  <li>product_id = 1</li>
  <li>file_name = maxgrand5.jpg</li>
  <li>file_path = assets</li>
  <li>product_attachment_id = 1</li>
  </ol>
  
  <p>one single product_id can have more than 1 product_attachment_id</p>
  
  <p>how can i get the data joined?</p>
  
  <p>I just updated my question, the problem is the second query is too late when i make the request, i should use promise to make it not late, how to do this?</p>