MCPcopy Create free account
hub / github.com/course-dasheng/fe-algorithm / asyncPool

Function asyncPool

interview/limit-promise.js:1–17  ·  view source on GitHub ↗
({
  limit,
  items,
  fn
})

Source from the content-addressed store, hash-verified

1async function asyncPool({
2 limit,
3 items,
4 fn
5}) {
6 const promises= []
7 const pool = new Set()
8 for (const item of items) {
9 const promise = fn(item)
10 promises.push(promise)
11 pool.add(promise)
12 const clean = () => pool.delete(promise)
13 promise.then(clean, clean)
14 if (pool.size >= limit) await Promise.race(pool)
15 }
16 return Promise.all(promises)
17}
18
19async function sleep(n,name='test'){
20 return new Promise(resolve=>{

Callers 1

startFunction · 0.85

Calls 2

fnFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected