-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathbatchFetch.js
33 lines (29 loc) · 1.08 KB
/
batchFetch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import PQueue from 'p-queue';
import axios from "../libs_drpy/axios.min";
// p-queue 的实现,不怎么兼容海阔的旧版
globalThis.batchFetch = async (items, maxWorkers = 5, timeoutConfig = 5000) => {
const queue = new PQueue({concurrency: maxWorkers});
// 获取全局 timeout 设置
const timeout = timeoutConfig;
// 遍历 items 并生成任务队列
const promises = items.map((item) => {
return queue.add(async () => {
try {
const response = await axios(
Object.assign({}, item?.options, {
url: item.url,
method: item?.options?.method || 'GET',
timeout: item?.options?.timeout || timeout,
responseType: 'text',
}),
);
return response.data;
} catch (error) {
console.log(`[batchFetch][error] ${item.url}: ${error}`);
return null;
}
});
});
// 执行所有任务
return Promise.all(promises);
};