01 · TypeScript concurrency pool
Awaiting model testModel output and observed result: not available; no model run has been performed. These checks are authored fixtures, not model output.
Prompt to give the model
Implement pool.ts exporting async function pool<T>(tasks: Array<() => Promise<T>>, limit: number): Promise<PromiseSettledResult<T>[]>. Start tasks lazily, run at most limit simultaneously, keep input order, settle promise rejection and synchronous throws without stopping other tasks. Empty input returns []. Reject non-positive/non-integer limits with RangeError even for empty input. Use no dependencies. Return only pool.ts. Do not modify the supplied checks.Input / starter files
// Input: delays [30, 5, 15] ms; limit 2.
// Implementation file: pool.tsSave these checks before testing
// Save as pool.check.ts (Node 22+)
import assert from 'node:assert/strict';
import { pool } from './pool.ts';
let active = 0, peak = 0;
const tasks = [30, 5, 15].map((ms, i) => async () => {
active++; peak = Math.max(peak, active);
await new Promise(r => setTimeout(r, ms));
active--; return i;
});
assert.deepEqual(await pool(tasks, 2), [0,1,2].map(value => ({status:'fulfilled', value})));
assert.equal(peak, 2);
const rejected = await pool([() => { throw new Error('sync'); }, () => Promise.reject('async'), async () => 7], 1);
assert.deepEqual(rejected.map(r => r.status), ['rejected','rejected','fulfilled']);
assert.deepEqual(rejected[2], {status:'fulfilled', value:7});
assert.deepEqual(await pool([], 2), []);
for (const limit of [0, -1, 1.5, NaN, Infinity]) {
await assert.rejects(async () => pool([], limit), RangeError);
}
console.log('pool: all assertions passed');Run locally
npx --yes --package tsx@4.21.0 tsx pool.check.tsExpected behavior / acceptance
Results remain [0, 1, 2], peak concurrency equals 2, and rejection does not stop later tasks. Empty input and invalid limits pass. Expected final line: pool: all assertions passed. Stop a hung run after 30 seconds and record a timeout.