← run

js-09-pool

1.000
7/7 tests· concurrency
Challenge · difficulty 5/5
# Concurrency-limited async pool

Implement an ES module **`solution.js`** (no external libraries):

```js
export async function pool(thunks, concurrency) { /* ... */ }
```

`thunks` is an array of zero-argument functions, each returning a promise (async thunks).
Run them with **at most `concurrency` running at the same time**, and resolve with an array
of their results **in the original order of `thunks`** (not completion order).

Requirements:
- The result at index `i` must be the resolved value of `thunks[i]`.
- At no point may more than `concurrency` thunks be in flight simultaneously.
- As soon as one thunk settles, the next pending thunk should start (keep the pool full).
- An empty `thunks` array resolves to `[]`.
- You may assume `concurrency >= 1`.

Notes:
- Do not simply run everything via `Promise.all(thunks.map(...))` — that ignores the limit.
- You do not need to handle rejections specially (a rejecting thunk may reject the pool).

Example:
```js
const order = [];
const make = (id, ms) => () =>
  new Promise((res) => setTimeout(() => { order.push(id); res(id); }, ms));
await pool([make("a", 30), make("b", 10), make("c", 20)], 2);
// => ["a", "b", "c"]  (results in original order, regardless of finish order)
```
tests/solution.test.js
import { test } from "node:test";
import { strict as assert } from "node:assert";
import { pool } from "./solution.js";

const sleep = (ms) => new Promise((res) => setTimeout(res, ms));

// Build instrumented thunks that track peak concurrency.
function makeTracker() {
  const state = { active: 0, peak: 0, starts: [] };
  const thunk = (id, ms) => async () => {
    state.active++;
    state.peak = Math.max(state.peak, state.active);
    state.starts.push(id);
    await sleep(ms);
    state.active--;
    return id;
  };
  return { state, thunk };
}

test("results are returned in original order, not completion order", async () => {
  const { thunk } = makeTracker();
  // 'a' is slowest but must still come first in the results.
  const out = await pool([thunk("a", 30), thunk("b", 5), thunk("c", 15)], 3);
  assert.deepEqual(out, ["a", "b", "c"]);
});

test("peak concurrency never exceeds the limit", async () => {
  const { state, thunk } = makeTracker();
  const thunks = [];
  for (let i = 0; i < 10; i++) thunks.push(thunk(i, 10));
  const out = await pool(thunks, 3);
  assert.deepEqual(out, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
  assert.ok(state.peak <= 3, `peak was ${state.peak}, expected <= 3`);
  assert.equal(state.peak, 3); // pool should actually reach the limit
});

test("concurrency of 1 runs strictly sequentially", async () => {
  const { state, thunk } = makeTracker();
  const thunks = [thunk("x", 10), thunk("y", 10), thunk("z", 10)];
  const out = await pool(thunks, 1);
  assert.deepEqual(out, ["x", "y", "z"]);
  assert.equal(state.peak, 1);
});

test("empty thunks -> empty array", async () => {
  const out = await pool([], 4);
  assert.deepEqual(out, []);
});

test("concurrency larger than number of thunks", async () => {
  const { state, thunk } = makeTracker();
  const thunks = [thunk(1, 5), thunk(2, 5)];
  const out = await pool(thunks, 10);
  assert.deepEqual(out, [1, 2]);
  assert.equal(state.peak, 2);
});

test("all thunks actually run exactly once", async () => {
  let count = 0;
  const thunks = [];
  for (let i = 0; i < 6; i++) {
    thunks.push(async () => {
      count++;
      await sleep(2);
      return i * i;
    });
  }
  const out = await pool(thunks, 2);
  assert.deepEqual(out, [0, 1, 4, 9, 16, 25]);
  assert.equal(count, 6);
});

test("pool keeps the slots full as tasks finish", async () => {
  // Mix of fast and slow tasks; with limit 2, a fast finisher should let a new
  // task start while a slow one is still running -> peak hits 2 repeatedly.
  const { state, thunk } = makeTracker();
  const thunks = [
    thunk("a", 40),
    thunk("b", 5),
    thunk("c", 5),
    thunk("d", 5),
  ];
  const out = await pool(thunks, 2);
  assert.deepEqual(out, ["a", "b", "c", "d"]);
  assert.equal(state.peak, 2);
});
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.
Proposed solution
```js
export async function pool(thunks, concurrency) {
  const results = new Array(thunks.length);
  const active = new Set();

  for (let i = 0; i < thunks.length; i++) {
    const index = i;
    const thunk = thunks[i];

    const promise = thunk().then(
      (result) => {
        results[index] = result;
        active.delete(promise);
      },
      (error) => {
        active.delete(promise);
        throw error;
      }
    );

    active.add(promise);

    if (active.size >= concurrency) {
      await Promise.race(active);
    }
  }

  await Promise.all(active);
  return results;
}
```

Test output (stdout)

TAP version 13
# Subtest: results are returned in original order, not completion order
ok 1 - results are returned in original order, not completion order
  ---
  duration_ms: 30.569872
  type: 'test'
  ...
# Subtest: peak concurrency never exceeds the limit
ok 2 - peak concurrency never exceeds the limit
  ---
  duration_ms: 42.448565
  type: 'test'
  ...
# Subtest: concurrency of 1 runs strictly sequentially
ok 3 - concurrency of 1 runs strictly sequentially
  ---
  duration_ms: 32.673894
  type: 'test'
  ...
# Subtest: empty thunks -> empty array
ok 4 - empty thunks -> empty array
  ---
  duration_ms: 0.214632
  type: 'test'
  ...
# Subtest: concurrency larger than number of thunks
ok 5 - concurrency larger than number of thunks
  ---
  duration_ms: 6.097968
  type: 'test'
  ...
# Subtest: all thunks actually run exactly once
ok 6 - all thunks actually run exactly once
  ---
  duration_ms: 6.60231
  type: 'test'
  ...
# Subtest: pool keeps the slots full as tasks finish
ok 7 - pool keeps the slots full as tasks finish
  ---
  duration_ms: 40.872265
  type: 'test'
  ...
1..7
# tests 7
# suites 0
# pass 7
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 184.471834