-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现简单的并发请求控制 #24
Comments
First version: function sendRequest(urls, max, callback) {
const len = urls.length;
let currentIdx = Math.min(len, max);
let counter = 0;
function _done() {
counter += 1;
// All done.
if (counter === len) {
return callback();
}
// Append to queue.
_fetch(urls[currentIdx++]);
}
function _fetch(url) {
fetch(url).finally(() => {
_done();
});
}
for (var i = 0; i < currentIdx; i++) {
_fetch(urls[i]);
}
} |
Usage: var urls = new Array(24).fill(true).map((x, i) => `https://github.com/hstarorg/HstarDoc/issues/${i + 1}`);
sendRequest(urls, 5, function() {
console.log('done');
}); |
Second version: function sendRequest(urls, max, callback) {
const len = urls.length;
let currentIdx = Math.min(len, max);
let counter = 0;
function _done() {
counter += 1;
// All done.
if (counter === len) {
return callback();
}
// Append to queue.
if (currentIdx < len) {
_fetch(urls[currentIdx++]);
}
}
function _fetch(url) {
fetch(url).finally(() => {
_done();
});
}
for (var i = 0; i < currentIdx; i++) {
_fetch(urls[i]);
}
} |
Another version: function sendRequest(urls, max, callback) {
const len = urls.length;
let idx = 0;
let counter = 0;
function _request() {
// 有请求,有通道
while (idx < len && max > 0) {
max--; // 占用通道
fetch(urls[idx++]).finally(() => {
max++; // 释放通道
counter++;
if (counter === len) {
return callback();
} else {
_request();
}
});
}
}
_request();
} |
幻神,请问这道题能不能用RxJS解啊,思路又是怎样的。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
题目:请实现如下的函数,可以批量请求数据,所有的URL地址在 urls 参数中,同时可以通过 max 参数控制请求的并发数,当所有的请求结束之后,需要执行 callback 回调函数。发请求的函数可以直接使用 fetch 即可。
The text was updated successfully, but these errors were encountered: