I am using Google geocoding in my code. I have to geocode about 50 places,
but Google doesn't allow to geocode so many places at the same time, generating a 'OVER_QUERY_LIMIT' error.
So I want do delay the geocoding for few seconds when I exceed the quota.
I have a function geocode()
that returns a promise
. When I exceed quota, I want to recursively call itself.
This is my not-working code:
function geocodeAll(addresses)
{
for (addr in addresses)
{
geocode(addresses[addr])
.then(
function(coord)
{/* I don't always get here */}
)
}
}
function geocode(address)
{
var deferred = Q.defer();
geocoder.geocode(address, function ( err, geoData )
{
if (err)
{ deferred.reject(err);}
else
{
if (geoData.status=="OVER_QUERY_LIMIT" )
{ // doh! quota exceeded, delay the promise
setTimeout(function()
{geocode(address)
.then(
function(coord)
{deferred.resolve(coord);}
);
}, 1000);
}
else
{ // everything ok
var coord = {'lat':geoData.lat, 'lng':geoData.lng};
deferred.resolve(coord);
}
}
});
return deferred.promise;
}
UPDATE [solved]
actually the code is right. I had an uncaughtException not related to the delay. Using Q.all([..]).then().catch()
I found it