-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathhikerBatchFetch.js
36 lines (31 loc) · 1.17 KB
/
hikerBatchFetch.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
34
35
36
import DsQueue from './dsQueue.js';
import axios from './axios.min.js';
export const batchFetch4 = async (items, maxWorkers = 5, timeoutConfig = 5000) => {
let t1 = (new Date()).getTime();
const queue = new DsQueue(maxWorkers);
// 获取全局 timeout 设置
const timeout = timeoutConfig;
const results = [];
items.forEach((item, index) => {
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',
}),
);
results[index] = response.data;
} catch (error) {
console.log(`[batchFetch][error] ${item.url}: ${error}`);
results[index] = null;
}
});
});
await queue.onIdle();
let t2 = (new Date()).getTime();
log(`DsQueue 批量请求 ${items[0].url} 等 ${items.length}个地址 耗时${t2 - t1}毫秒:`);
return results;
};