title: Node wait all async call before execute code
body: <p>I struggle for some days on a quite simple problem i guess.</p>
  
  <p>I have multiple call like this </p>
  
  <pre><code>for(var i = 1; i &lt;= timeToIterate;i++){
      xapi.fetch({})
          .then(function (response) {
              response.results.forEach(y =&gt; {
                  yapi.fetch({})
                      .then(function (response2){
                          //some treatment here with
                          response.articles.forEach(article =&gt; {
                              finalArray.push({})
                          });
                      })
          });
  }
  </code></pre>
  
  <p>No problem until then everything works fine but now i want to execute code with my finalArray after everything is finished. but when i tried to put a then after the second then but it don't execute well i tried to put after the for loop but still empty. I even tried with some await but i can't figure out how to execute code when all the then are finished.</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT :  more test with the answer</p>
  
  <pre><code>var count = 0; //just to know how many articles
  var completeArray = [];
  var promise_arr = [];
  xapi.fetch({})
      .then(async function (responseJson) {
          var timeToIterate = Math.ceil(responseJson.totalResultNumber / 50);
          for(var i = 1; i &lt;= timeToIterate;i++){
              p = yapi.fetch({})
                  .then(function (responseJson) {
                      if (responseJson.results.length &gt; 0){
                              responseJson.results.forEach(x =&gt; {
                                  zapi.fetch({})
                                      .then(function (response) {                            
                    response.articles.forEach(article =&gt;{
                                           count++;
                                           console.log(count);
                                           completeArray.push({article};
  
                                       });
  
                                  }).catch(e =&gt; {
                                      console.log(e);
                                   })
                              });
                          }
                      }).catch(e =&gt; {
                          console.log(e);
                       })
                       promise_arr.push(p);
                  }
                  await (Promise.all(promise_arr));
                  console.log('now '+ completeArray.length);
                  const  insert = elasticClient.bulk(
                      {body: completeArray},function (err, resp) {
                          completeArray = [];
                          if(resp.errors) {                                   
                              console.log(JSON.stringify(resp, null, '\t'));
                          }
                      });
              }).catch(e =&gt; {
                  console.log(e);
               }).then(function(){
                  return res.json(count);
              })
  </code></pre>
  
  <p>But it still trigger before the end of the for function. Did i miss something ? I will try with the second answer !</p>
  
  <p>EDIT 2 : I tried the second answer too but can't find out how to get it work without error, i'm new with bluebird didn't knew it before today</p>
  
  <pre><code>var count = 0;
  var completeArray = [];
  let response0 = await api1.fetch({});
  
  await Promise.map(response0.results, async z =&gt; {
      console.log(z); //Good log here
      var timeToIterate = Math.ceil(z.totalResultNumber / 50);
                  for (var i = 1; i &lt;= timeToIterate; i++) {
                      let response = await api2.fetch({});
  
                      await Promise.map(response.results, async x =&gt; {
                          console.log(x) // No Log here
                          let response2 = await api3.fetch({});
  
                          await Promise.map(response2, y =&gt; {
                              if (y.sections &amp;&amp; y.sections.length &gt; 0) {
                                  y.sections.forEach(section =&gt; {
                                      if (section.articles.length &gt; 0) {                                    
                                          lawSection.articles.forEach(article =&gt; {
                                              count++;
                                              console.log(count);
                                              completeArray.push(article)
                                          });
                                      }
                                  })
                              } else if (y.articles &amp;&amp; y.articles.length &gt; 0) {
                                  y.articles.forEach(article =&gt; {
                                      count++;
                                      console.log(count);
                                      completeArray.push(article);
                                  });
                              }
                          });
                      });
                  }
                  console.log('now ' + completeArray.length); // got this log Z time but nothing above so completeArray.length always = 0
                  const insert = elasticClient.bulk({
                      body: completeArray
                  }, function (err, resp) {
                      completeArray = [];
                      if (resp.errors) {
                          console.log(JSON.stringify(resp, null, '\t'));
                      }
                  });
              });
  </code></pre>
  
  <p>EDIT 3 : Thanks to @tadman i figure it out but still have a problem of asynchronous for my elastic client here is my code</p>
  
  <pre><code>var count = 0;
  var completeArray = [];
  let response0 = await api1.fetch({});
      await Promise.map(response0.results, async z =&gt; {
          var timeToIterate = Math.ceil(response0.totalResultNumber / 50);
              for (var i = 1; i &lt;= timeToIterate; i++) {
                  let response = await api2.fetch({});
                  await Promise.map(response.results, async x =&gt; {
                      if(response2.sections &amp;&amp; response2.sections.length &gt; 0){
                          response2.sections.forEach(section =&gt; {
                              if (section.articles.length &gt; 0) {
                                  section.articles.forEach(article =&gt; {
                                      count++;
                                      console.log(count);
                                      completeArray.push(article);
                                  });
                              }
                          })
                      } else if (response2.articles &amp;&amp; response2.articles.length &gt; 0) {
                          response2.articles.forEach(article =&gt; {
                              count++;
                              console.log(count);
                              completeArray.push(article)
                          });
                      }
                  });
                  // I have to put it here because if i put it after the for loop the request http is to big and i have a 413 response. But the problem here is it didn't wait for the callback for the next 'for' loop iteration ! So i have the right number with my 'count' variable but didn't have the right number in my database 
                  console.log('now ' + completeArray.length);
                  await elasticClient.bulk( //how can i wait for the end of this and apply the completeArray = [] before the next iteration ?
                      {refresh: "wait_for",body: completeArray},
                      async function(err, response) {
                          completeArray = [];
                      if (err) { console.log(err); return; }
                      console.log(`Inside bulk3...`);
                      let errorCount = 0;
                      response.items.forEach(item =&gt; {
                          if (item.index &amp;&amp; item.index.error) {
                          console.log(++errorCount, item.index.error);
                          }
                      });
                      console.log(`Successfully indexed items`);
                      }
                  )
              }
          });
  </code></pre>