MCPcopy Create free account
hub / github.com/chavyleung/scripts / limitConcurrency

Function limitConcurrency

box/chavy.boxjs.js:1067–1090  ·  view source on GitHub ↗
(tasks, limit)

Source from the content-addressed store, hash-verified

1065
1066// 自定义并发控制函数
1067async function limitConcurrency(tasks, limit) {
1068 const results = [];
1069 const executing = [];
1070
1071 for (const task of tasks) {
1072 const promise = task(); // 执行任务
1073 results.push(promise);
1074
1075 if (executing.length >= limit) {
1076 await Promise.race(executing);
1077 }
1078
1079 executing.push(promise);
1080 promise.then(() => {
1081 const index = executing.indexOf(promise);
1082 if (index !== -1) executing.splice(index, 1);
1083 }).catch(() => {
1084 const index = executing.indexOf(promise);
1085 if (index !== -1) executing.splice(index, 1);
1086 });
1087 }
1088
1089 return Promise.all(results);
1090}
1091
1092async function reloadAppSubCaches() {
1093 $.msg($.name, '更新订阅: 开始!');

Callers 1

reloadAppSubCachesFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected