title: Javascript - How to use promises with multiple .then()
body: <p>I'm trying to create a array structure. So, we have parts and each part had articles.</p>
  
  <p>So i did a promise to collect all parts <code>.then()</code> i need to iterate part promise and select the articles in this part <code>.then()</code> i want to push this to a array parts and render this in a view.</p>
  
  <p>The structure is this:</p>
  
  <pre><code>-PARTS
        - part
              - u_order
              - u_familia
              - u_part
              - u_type
              - articles (article from each part)
  </code></pre>
  
  <p>And my code is this:</p>
  
  <pre><code>var p1 = new Promise(function(resolve, reject) {
        var stamp = req.params.stamp;
        request.query("SELECT u_order, u_familia, u_part, u_type FROM u_part (nolock) where u_order &lt;'92' and u_order &lt;&gt; '100'").then((data)=&gt;resolve(data));
        // or
        // reject ("Error!");
      });
  
  
  
      p1.then(function(value){
        var stamp = req.params.stamp;
        console.log(value.length);
        for(var i= 0; i&lt;value.length; i++)
        {
          console.log(value[i]);
          request.query("SELECT st.u_posic, sc.ref, sc.qtt, sc.design FROM st INNER JOIN sc ON st.ref = sc.ref where sc.ststamp ='"+stamp+"' and st.u_posic = '"+value[i].u_order+"'").then((data)=&gt;resolve(data));
        }
  
      }, function(reason){
        console.log(reason);
      });
  
      p1.then(function(part, articles){
        var parts = [];
        console.log("PART: " +part.length);
        for(var j= 0; j&lt;part.length; j++)
        {
          console.log(part[j].u_order);
          console.log(part[j].u_familia);
          console.log(part[j].u_part);
          console.log(part[j].u_type);
          console.log(articles[j]);
        };
      });
  </code></pre>
  
  <p>In last <code>.then()</code> i just have the parts, i can't access to articles maybe because i'm not doing well with second <code>.then()</code>
  I'm starting working with promises, i also read documentation but i can't do this.</p>
  
  <p>Anyone can help me to understand and solve this?</p>
  
  <p>Thank you</p>