提出多个获取请求,但限制为10个请求/分钟nodejs
我很快就会解释我的问题。 我正在使用nodejs从URL中检索响应,我必须向该URL提出多个请求。
我只有一个限制:每分钟我不能提出10个以上的请求。 我该如何处理这个问题?
我也试图也遵循堆栈溢出响应:向只能处理20个请求的API请求几分钟请求,
但是该方法不起作用,因为它不等待所有承诺及其给出未定义的结果,
实际上我正在使用该代码,但不是等待所有响应,它使我直接不确定,以后所有请求都完成了:
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);
}
})();
}
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);
}
})();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过实现
Promise.all
方法来做。例如:
对于场景2,我们在下面的评论部分讨论
You can do by implementing
Promise.all
method.For example:
For Scenario 2 which we discuss below in comment section