I’m trying to save some information from a web service, it is a lot of data and I have to make a lot of Ajax requests, after many requests so quick, the server justs throws "Too many requests" which makes sense…
My code looks something like this:
function getDataFromWS()
{
define some vars
$ make an ajax request to MY database to get every item i will request on the external WS
(that query returns like 150 items to arrayFromResponse)
//Loop into the array with 150 items
arrayFromResponse.forEach((element)=>{
//For loop to make request for each day of the month e.g. August 31 days
for(let i = 1; i <= 31; i++){
//change uri to match each day using i
uriData.date = '2020-08-0'+i;
//This is where after many requests it throws error 429 "Too many"
$.ajax({
data: uriData,
url: uri,
type: 'GET',
async: false,
success : function(response){
//Save data from response to some arrays I use
},
error: function(response){
console.log(response);
console.log('Error');
}
});
}
})
//After all that looping and requests make a CSV file
buildCSVfile(dataFromResponses);
}
I need all this code to be synchronous, I assume that I need to use something like setTimeout or so because I tried it but then the buildCSVfile() function executes before the loops, I guess the delay should be after each ajax request in the date for loop. Maybe 10 seconds per request so it won’t say too many requests? All the core code is in the success function. I don’t need this to be fast, just to make sure to get all the info, per item and per day of the month.
I appreciate any help given.
2
Answers
I suggest using jQuery Deffered since you already use jQuery.
Read up on jQuery Deferred to understand the inner workings.
It basically implements a system based on Promises.
Your code could end up something like:
I believe this is a challenge for backend- its API should support doing this task by one request.
But if you’re sure that you need to make many separate requests, just use concurrency limit (Live demo):