2024年2月20日发(作者:)
基于回调函数function callAsyncParallel(hookName, done, ...args) { const fns = hooks[hookName] let count = let _done = () => { count-- if (count === 0) { done() } } h(fn => fn(...args, _done))}//
限制并发数function callAsyncParallelN(hookName, done, N, ...args) { const fns = hooks[hookName] let count = let cur = 0 let limit = N < ? N : let _done = () => { count-- if (count === 0) { done() } else if (cur < ) { fns[cur++](...args, _done) } } for (; cur < limit; cur++) { fns[cur](...args, _done) }}
regHook('asyncParallel', (a, b, done) => { setTimeout(() => { ('asyncParallel 1', a, b); done() }, 1000) })regHook('asyncParallel', (a, b, done) => { setTimeout(() => { ('asyncParallel 2', a, b); done() }, 1000) })regHook('asyncParallel', (a, b, done) => { setTimeout(() => { ('asyncParallel 3', a, b); done() }, 1000) })callAsyncParallel('asyncParallel', () => { ('done') }, 'aa', 'bb')callAsyncParallelN('asyncParallel', () => { ('done') }, 2, 'aa', 'bb')
function callPromiseParallel(hookName, ...args) { return new Promise(resolve => { const fns = hooks[hookName] let count = let _done = () => { count-- if (count === 0) { resolve() } } h(fn => fn(...args).then(_done)) })}//
限制并发数function callPromiseParallelN(hookName, N, ...args) { return new Promise(resolve => { const fns = hooks[hookName] let count = let cur = 0 let limit = N < ? N : let _done = () => { count-- if (count === 0) { resolve() } else {
} for (; cur < limit; cur++) { fns[cur](...args).then(_done) } }) }}


发布评论