MCPcopy
hub / github.com/simstudioai/sim / settleWithConcurrency

Function settleWithConcurrency

apps/sim/app/api/mcp/tools/discover/route.ts:22–51  ·  view source on GitHub ↗
(
  items: T[],
  concurrency: number,
  task: (item: T) => Promise<R>
)

Source from the content-addressed store, hash-verified

20export const dynamic = 'force-dynamic'
21
22async function settleWithConcurrency<T, R>(
23 items: T[],
24 concurrency: number,
25 task: (item: T) => Promise<R>
26): Promise<Array<PromiseSettledResult<R>>> {
27 const results: Array<PromiseSettledResult<R> | undefined> = new Array(items.length)
28 let nextIndex = 0
29
30 const workers = Array.from({ length: Math.min(concurrency, items.length) }, async () => {
31 while (nextIndex < items.length) {
32 const index = nextIndex
33 nextIndex += 1
34 try {
35 results[index] = { status: 'fulfilled', value: await task(items[index]) }
36 } catch (reason) {
37 results[index] = { status: 'rejected', reason }
38 }
39 }
40 })
41
42 await Promise.all(workers)
43
44 return results.map(
45 (result) =>
46 result ?? {
47 status: 'rejected',
48 reason: new Error('MCP refresh discovery task did not run'),
49 }
50 )
51}
52
53export const GET = withRouteHandler(
54 withMcpAuth('read')(async (request: NextRequest, { userId, workspaceId, requestId }) => {

Callers 1

route.tsFile · 0.85

Calls 1

taskFunction · 0.85

Tested by

no test coverage detected