I have the following code where I am trying to convert a list of 10-15 addresses in a businesses array to lat / lng coordinates using the 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() {
The problem is that the Google API sometimes returns an OVER_QUERY_LIMIT error, so I was wondering if there was a way to re-execute a promise that didn’t pass after a few seconds. In the above example, the else if catches the error, so how can I do it again after a timeout?
Please note that I just started using promises and have not yet figured out how they work, so my approach may be completely wrong. Also, all this is done under the assumption that the Google API will not allow me to resolve addresses in bulk, if this is not so, please let me know.
javascript promise google-geocoding-api
Mihail russu
source share