[英]Make multiple get request but with a minute limit of 10 request/minute NodeJS
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.
我正在使用 NodeJS 从 url 检索响应,我必须向该 URL 发出多个请求。
I got only one limit: I can't make more than 10 request for each minute.我只有一个限制:我每分钟不能发出超过 10 个请求。 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我也尝试遵循该堆栈溢出响应: 向 API 发出多个请求,每分钟只能处理 20 个请求
but that method its not working because its not awaiting all promise and its giving undefined result但该方法不起作用,因为它不等待所有 promise 并给出未定义的结果
Actually I am using that code but its not awaiting all response, its giving me undefined directly and later all requests are done:实际上我正在使用该代码但它没有等待所有响应,它直接给我 undefined 然后所有请求都完成了:
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);
}
})();
} }
You can do by implementing Promise.all
method.您可以通过实施
Promise.all
方法来完成。
For example:例如:
const [ response1, response2, response3 ] = await Promise.all([ axios.get(URL_HERE), axios.get(URL_HERE), axios.get(URL_HERE) ];
const [response1, response2, response3] = await Promise.all([ axios.get(URL_HERE), axios.get(URL_HERE), axios.get(URL_HERE) ];
console.log(response1);
控制台日志(响应1);
console.log(response3);
控制台日志(响应3);
console.log(response2);
控制台日志(响应2);
For Scenario 2 which we discuss below in comment section对于我们在评论部分下面讨论的场景 2
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.