Reputation: 23
I'll explain my problem shortly. I am using NodeJS to retrieve response from a url and I have to make multiple request to that URL.
I got only one limit: I can't make more than 10 request for each minute. How can I handle that problem?
I tried to follow that stack overflow response too: Make several requests to an API that can only handle 20 request a minute
but that method its not working because its not awaiting all promise and its giving undefined result
Actually I am using that code but its not awaiting all response, its giving me undefined directly and later all requests are done:
async function rateLimitedRequests(array, chunkSize) {
var delay = 3000 * chunkSize;
var remaining = array.length;
var promises = [];
var addPromises = async function(newPromises) {
Array.prototype.push.apply(promises, newPromises);
if (remaining -= newPromises.length == 0) {
await Promise.all(promises).then((data) => {
console.log(data);
});
}
};
(async function request() {
addPromises(array.splice(0, chunkSize).map(apiFetch));
if (array.length) {
setTimeout(request, delay);
}
})();
}
Upvotes: 0
Views: 575
Reputation: 98
You can do by implementing Promise.all
method.
For example:
const [ response1, response2, response3 ] = await Promise.all([ axios.get(URL_HERE), axios.get(URL_HERE), axios.get(URL_HERE) ];
console.log(response1);
console.log(response3);
console.log(response2);
For Scenario 2 which we discuss below in comment section
function rateLimiter(array = [], chunkSize = 10 ) {
const delay = 10 * 1000;
return new Promise((resolve, reject) => {
let results = [];
const callback = (iteration = 0) => {
const process = array.slice(iteration * chunkSize, chunkSize);
if(!process.length) return resolve(results);
Promise.all(process).then(responses => {
results = [ ...results, ...responses ];
const processLength = array.slice((iteration + 1) * chunkSize, chunkSize).length;
if(!processLength) return resolve(results);
setTimeout(() => {
callback(iteration + 1);
}, delay)
})
}
callback();
})
}
let results = await rateLimiter([ axios.get('URL_HERE'), axios.get('URL_HERE') ], 20);
Upvotes: 1