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

Function limitConcurrency

chavy.box.js:1067–1092  ·  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
1081 .then(() => {
1082 const index = executing.indexOf(promise)
1083 if (index !== -1) executing.splice(index, 1)
1084 })
1085 .catch(() => {
1086 const index = executing.indexOf(promise)
1087 if (index !== -1) executing.splice(index, 1)
1088 })
1089 }
1090
1091 return Promise.all(results)
1092}
1093
1094async function reloadAppSubCaches() {
1095 $.msg($.name, '更新订阅: 开始!')

Callers 1

reloadAppSubCachesFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected