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.
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.
var currentBatch = 0;
while(currentBatch < 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 < batch * batchSize + batchSize; j++) {
if (j < 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);
});
});
}
But these calls are still executed asynchronously because they aren't chained with .then()
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 (here and here)