title: For loop and mysql node how to wait for all queries to finish to process view
body: <p>I'am struggling on a problem with node-mysql and a for loop.
  Basically what i'am trying to do is to get a Array or Object of </p>
  
  <pre><code>---------- ----------
  URL       | COUNT
  ---------- ----------
  </code></pre>
  
  <p>From my database, URL are pictures to be used as background and count is a INT.</p>
  
  <p><strong>The problem is : Node don't wait for all the queries / answer of MYSQL to process and render the view with the callback, thus, i'am getting a empty array on my view.</strong></p>
  
  <p>I need to do this inside a FOR LOOP since my search parameter for the query is inside a array like : </p>
  
  <pre><code>415 | 416  ...
  </code></pre>
  
  <p>(it's a list of documents id to search).</p>
  
  <pre><code>app.get('/next2', function(req, res) {
      function callback (s6) {
          cards = s6;
          res.render('step2.ejs', {
              DocumentsNextPage: DocumentsNextPage,
              cards: cards
          });
      };
  
      for (i = 0; i &lt; DocumentsNextPage.length; i++) {
          connection.query('SELECT DISTINCT url FROM documents WHERE documents.id = ?; SELECT COUNT(DISTINCT metas.name) AS cnt FROM documents, metas, documents_has_metas WHERE documents.id = documents_has_metas.documents_id AND metas.id_meta = documents_has_metas.metas_id AND documents.id = ?', [DocumentsNextPage[i], DocumentsNextPage[i]], function(err, results) {
              if (err) throw err;
              s6.push(results[0][0].url, results[1][0].cnt);
          });
      };
      callback(s6);
  });
  </code></pre>