Open QuocVi1994 opened 3 years ago
async function batchGet(urls, batchnum = 3, timeout = 3000) {
let ret = [];
while (urls.length > 0) {
var preList = urls.splice(0, batchnum);
let requestList = preList.map((url) => {
return request(url, timeout);
});
const result = await Promise.allsettled(requestList);
ret.concat(
result.map((item) => {
if (item.status === "rejected") {
return null;
} else {
return item.value;
}
})
);
}
return ret;
}
function request(url, timeout) {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject();
}, timeout);
// ajax发送请求
ajax({ url }, (data) => {
resolve(data);
});
});
}
// urls为一个不定长的数组
batchGet(["http1", "http2", "http3"]);