I have the following code where I am trying to convert a list of 10-15 addresses in the businesses
array into lat/lng coordinates using Google Maps Geocoding API.
var geocoder = new google.maps.Geocoder();
var proms = businesses.map(function(address) {
return prom = new Promise(function(resolve) {
geocoder.geocode({
address: address
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
resolve({
results: results,
business: address
});
} else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
setTimeout(function() {
//HOW DO I RE-RUN THIS "new Promise"?
}, 1000);
} else {
console.log(status + ': ' + results);
}
});
});
});
Promise.all(proms);
The problem is that Google's API will sometimes return an OVER_QUERY_LIMIT
error so I was wondering if there's a way to re-run the promise that failed after a few seconds. In the example above the else if
statement catches the error, so how would I make it rerun after a timeout?
Note that I just started using promises and don't have a good understanding yet how they work so my approach might be completely wrong. Also all of this is done under the assumption that Google's API won't let me resolve addresses in bulk, if that's not the case - please, let me know.