← run

js-09-pool

0.000
0/1 tests· concurrency

Proposed solution

```js
export async function pool(thunks, concurrency) {
  const results = new Array(thunks.length);
  let inFlight = 0;
  let nextIndex = 0;

  const enqueue = (thunk, index) => {
    results[index] = thunk();
    inFlight++;
  };

  const processQueue = () => {
    while (inFlight < concurrency && nextIndex < thunks.length) {
      enqueue(thunks[nextIndex++]);
    }
  };

  const cleanup = () => {
    inFlight = 0;
    processQueue();
  };

  const cleanupPromise = new Promise(cleanup);

  const promiseChain = new Promise((resolve, reject) => {
    const processNext = () => {
      if (nextIndex >= thunks.length) {
        cleanupPromise.then(resolve, reject);
        return;
      }

      processQueue();

      Promise.race(results.slice(nextIndex)).then((result) => {
        enqueue(thunks[nextIndex++], nextIndex);
        processNext();
      });
    };

    processNext();
  });

  return promiseChain;
}
```

Test output (stdout)

TAP version 13

Errors (stderr)


[TIMEOUT]
Self-repair attempts
[
  {
    "total": 1,
    "answer": "```js\nexport async function pool(thunks, concurrency) {\n  const results = new Array(thunks.length);\n  let inFlight = 0;\n  let nextIndex = 0;\n\n  const enqueue = (thunk, index) => {\n    results[index] = thunk();\n    inFlight++;\n  };\n\n  const processQueue = () => {\n    while (inFlight < concurrency && nextIndex < thunks.length) {\n      enqueue(thunks[nextIndex++]);\n    }\n  };\n\n  const cleanup = () => {\n    inFlight = 0;\n    processQueue();\n  };\n\n  const cleanupPromise = new Promise(cleanup);\n\n  const promiseChain = new Promise((resolve, reject) => {\n    const processNext = () => {\n      if (nextIndex >= thunks.length) {\n        cleanupPromise.then(resolve, reject);\n        return;\n      }\n\n      processQueue();\n\n      Promise.race(results.slice(nextIndex)).then((result) => {\n        enqueue(thunks[nextIndex++], nextIndex);\n        processNext();\n      });\n    };\n\n    processNext();\n  });\n\n  return promiseChain;\n}\n```",
    "passed": 0,
    "test_error": "TAP version 13\n\n\n[TIMEOUT]"
  },
  {
    "total": 1,
    "answer": "```js\nexport async function pool(thunks, concurrency) {\n  const results = new Array(thunks.length);\n  let inFlight = 0;\n  let nextIndex = 0;\n\n  const enqueue = (thunk, index) => {\n    results[index] = thunk();\n    inFlight++;\n  };\n\n  const processQueue = () => {\n    while (inFlight < concurrency && nextIndex < thunks.length) {\n      enqueue(thunks[nextIndex++]);\n    }\n  };\n\n  const cleanup = () => {\n    inFlight = 0;\n    nextIndex = 0;\n  };\n\n  const cleanupPromise = new Promise(cleanup);\n\n  const promiseChain = new Promise((resolve, reject) => {\n    const processNext = () => {\n      if (nextIndex >= thunks.length) {\n        cleanupPromise.then(resolve, reject);\n        return;\n      }\n\n      processQueue();\n\n      Promise.race(results.slice(nextIndex)).then((result) => {\n        enqueue(thunks[nextIndex++], nextIndex);\n        processNext();\n      });\n    };\n\n    processNext();\n  });\n\n  return promiseChain;\n}\n```",
    "passed": 0,
    "test_error": "TAP version 13\n\n\n[TIMEOUT]"
  },
  {
    "total": 1,
    "answer": "```js\nexport async function pool(thunks, concurrency) {\n  const results = new Array(thunks.length);\n  let inFlight = 0;\n  let nextIndex = 0;\n\n  const enqueue = (thunk, index) => {\n    results[index] = thunk();\n    inFlight++;\n  };\n\n  const processQueue = () => {\n    while (inFlight < concurrency && nextIndex < thunks.length) {\n      enqueue(thunks[nextIndex++]);\n    }\n  };\n\n  const cleanup = () => {\n    inFlight = 0;\n    nextIndex = 0;\n  };\n\n  const cleanupPromise = new Promise(cleanup);\n\n  const promiseChain = new Promise((resolve, reject) => {\n    const processNext = () => {\n      if (nextIndex >= thunks.length) {\n        cleanupPromise.then(resolve, reject);\n        return;\n      }\n\n      processQueue();\n\n      Promise.race(results.slice(nextIndex)).then((result) => {\n        enqueue(thunks[nextIndex++], nextIndex);\n        processNext();\n      });\n    };\n\n    processNext();\n  });\n\n  return promiseChain;\n}\n```",
    "passed": 0,
    "test_error": ""
  }
]
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.