Learning on Web Dev Open is free for all.

Interview Prep · CodeRun tasks with a concurrency limit
← Code

Run tasks with a concurrency limit

Medium30 minFree, no account

The async question that most people get almost right, and the ordering detail that catches them.


The question

Write pool(tasks, limit) where tasks is an array of functions returning promises.

Run at most limit at a time and resolve with the results in the original order.

If one rejects, the others should still finish and you should report which failed.

const tasks = urls.map(u => () => fetch(u).then(r => r.json()));
const results = await pool(tasks, 5);

// results[i] corresponds to tasks[i]
// [{ status: "fulfilled", value }, { status: "rejected", reason }, ...]
30:00Commit to an answer before you open the solution. Reading it first teaches you to recognise good answers, which is not the skill being tested.

Stuck?

0 of 3 hints taken

The worked solution

written by a person · not a grade

Score yourself

0 of 5 marked
  • Workers rather than batches, with the reason30
  • Results in original order20
  • A rejection does not abort the rest, and is reported25
  • Explained why tasks must be functions15
  • Handled limit > length and empty input10

We run no AI here and nothing on this page grades you. The score is yours, and the useful number is the one you get on the same problem a month from now, cold.

kept in this browser only