Recently, I've encountered an issue with the Reverse Geocode API from TomTom. The problem arises when I send multiple latitude and longitude coordinates (around 72 of them) to the API, resulting in frequent 429
responses.
To address this issue, I attempted to create a function that would introduce a 5
second delay before sending another request. TomTom recommends waiting for at least 5
seconds between requests.
I believed it was acceptable, as shown in the function below, to call one function (Topography.getTopography(latLng)
) and then use the result obtained after the then
call to feed into the TomTom request. Is there something wrong with this approach or could the issue lie within the setTimeout method?
Below is the code snippet of my function:
async function getTopographyData(latLang) {
const retryTimes = 5;
let counter = 0;
var newObj = {};
Topography.getTopography(latLang, options)
.then((results) => {
newObj.topography = results;
newObj.latlng = latLang;
return newObj;
})
.then(async (newObj) => {
var { lat, lng } = newObj.latlng;
let result = await axios.get(
`https://api.tomtom.com/search/2/reverseGeocode/crossStreet/${lat},${lng}.json?limit=1&spatialKeys=false&radius=10000&allowFreeformNewLine=false&view=Unified&key=${process.env.TOM_TOM_API}`
);
var { addresses } = result?.data;
var { address, position } = addresses[0];
var [lat, lng] = position.split(",").map((p) => +p);
newObj = {
...newObj,
latlng: { lat, lng },
address,
};
dispatch({ type: "setTopographyData", payload: newObj });
})
.catch(function (error) {
if (
(error.response?.status == 403 || error.response?.status == 429) &&
counter < retryTimes
) {
counter++;
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(getTopographyData(counter));
}, 5000);
});
} else {
console.log("error", error);
}
});
}
Any insights or suggestions would be greatly appreciated.
Further updates indicate that while implementing num8er's solution helped, it appears there might be a larger underlying issue with TomTom. At around 45 marker points (latitude and longitude objects), the reverseGeocode lookup stops functioning properly and starts displaying errors in the console. Additionally, clicking on the link provided in the console shows successful responses in the browser?! Strange behavior indeed!