title: Wait for all asynchronous calls before returning in ES5 pure JS?
body: <p>There are two asynchronous calls (@ <code>.then(...)</code>) in this function which get looped through multiple times. How can I restructure this function to ensure it only gets returns when all calls have been completed? I know I could use async/await in ES6 but I can only use ES5 with no libraries. </p>
  
  <pre><code>function queryLayer(layer, buffer){
    var query = layer.createQuery();
    query.geometry = buffer;
    query.spatialRelationship = "intersects";
  
    layer.queryFeatures(query).then(function(results){
      console.log(results.features[0].sourceLayer.title, results.features.length);
      if(!results.features.length &gt; 0) return false;
      var features = results.features;
      features.forEach(function(feature){
        feature.distance = calculateDistance(buffer.centroid, feature.geometry);
        locator.locationToAddress(feature.geometry).then(function(x){
          feature.address = x.address;
        });
      });
      return features;
    });
  }
  </code></pre>