title: Promise with a variable number of "then" in a for loop
body: <p>Trying to batch Google ElevationService's requests (and leaving a trail of questions on Stack Overflow) I'm now pointed towards Promise objects, which are completely new to me.</p>
  
  <p>Say I have a loop that runs 4 times, or in another case 7 times, or in yet another case 2 times; how would I implement the Promise aproach in that loop? At this moment this is my set up to try to get elevation data batched in 250 LatLng's a time for a given Google Maps DirectionsResponse.</p>
  
  <pre><code>var currentBatch = 0;
  while(currentBatch &lt; totalElevationBatches) {
      getRouteElevationChartDataBatch(currentBatch, batchSize);
      currentBatch++;
  }
  
  function getRouteElevationChartDataBatch(batch, batchSize) {
      return new Promise(function(resolve, reject) {
          var elevator = new google.maps.ElevationService();
          var thisBatchPath = [];
  
          for (var j = batch * batchSize; j &lt; batch * batchSize + batchSize; j++) {
              if (j &lt; directions.routes[0].overview_path.length) {
                  thisBatchPath.push(directions.routes[0].overview_path[j]);
              } else {
                  break;
              }
          }
  
          elevator.getElevationAlongPath({
              path: thisBatchPath,
              samples: 256
          }, function (elevations, status) {
              if (status != google.maps.ElevationStatus.OK) {
                  reject(status);
              }
  
              routeElevations = routeElevations.concat(elevations);
          });
      });
  }
  </code></pre>
  
  <p>But these calls are still executed asynchronously because they aren't chained with <code>.then()</code> method. How can I chain a variable number of chains and know when it's done? Sorry if this is a total stupid question; but I don't get it after consulting the documentation (<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="nofollow">here</a> and <a href="http://exploringjs.com/es6/ch_promises.html" rel="nofollow">here</a>)</p>