MCPcopy Create free account
hub / github.com/contentlayerdev/contentlayer / promiseMapPool

Function promiseMapPool

packages/@contentlayer/utils/src/promise.ts:23–43  ·  view source on GitHub ↗
(
  arr: T[],
  map: (el: T, index?: number) => Promise<Res>,
  poolLimit: number,
)

Source from the content-addressed store, hash-verified

21}
22
23export const promiseMapPool = async <T, Res>(
24 arr: T[],
25 map: (el: T, index?: number) => Promise<Res>,
26 poolLimit: number,
27): Promise<Res[]> => {
28 const ret: Promise<Res>[] = []
29 const executing: Promise<Res>[] = []
30 for (const [index, item] of arr.entries()) {
31 const p = Promise.resolve().then(() => map(item, index))
32 ret.push(p)
33
34 if (poolLimit <= arr.length) {
35 const e: any = p.then(() => executing.splice(executing.indexOf(e), 1))
36 executing.push(e)
37 if (executing.length >= poolLimit) {
38 await Promise.race(executing)
39 }
40 }
41 }
42 return Promise.all(ret)
43}

Callers

nothing calls this directly

Calls 1

mapFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…