/**
* Executes an array of asynchronous functions concurrently with a maximum number of parallel executions.
* @param fnList An array of functions that return a Promise.
* @param maxNum The maximum number of parallel executions.
* @returns A Promise that resolves to an array of results or errors from the executed functions.
*/
function curRequest(fnList: (() => Promise<any>)[], maxNum: number) {
if (!fnList.length) return Promise.resolve([]);
return new Promise(resolve => {
let index = 0;
// 记录已经完成的数量
let count = 0;
const result: any[] = [];
async function _request() {
const i = index;
const fn = fnList[index];
if (!fn) return null;
index++;
try {
const res = await fn();
result[i] = res;
} catch (e) {
result[i] = e;
} finally {
count++;
if (index < fnList.length) {
_request();
}
if (count === fnList.length) {
resolve(result);
}
}
}
for (let n = 0; n < Math.min(fnList.length, maxNum); n++) {
_request();
}
});
}
并发请求
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- jmeter当需要通过登录一批不同用户,并发请求一个接口时 首先在线程组中设置线程用户数,完成该线程所花的时间,一...
- 题目 实现一个请求n个url的函数, 并发的请求数不超过设定的throttle, 每结束一个发起新的一个请求, 要...
- 场景:部分页面数据量大,用户查询多 接口:用户查询数据列表 前置条件:需要先登录 解决方案:先准备用户登录,之后使...
- 对于一些用户请求,在某些情况下是可能重复发送的,如果是查询类操作并无大碍,但其中有些是涉及写入操作的,一旦重复了,...
- 一、问题 昨晚(2020-07-09)八点左右,生产运行的APP大面积上报请求超时,无法获取数据,显示网络连接错误...