Problem, as an interviewer would state it:
"Implement pLimit(concurrency) — it returns a function you wrap around any async task, guaranteeing no more than concurrency tasks are ever running at the same time, queueing the rest until a slot frees up."
Examples:
const limit = pLimit(2);
await Promise.all([1,2,3,4,5].map(id => limit(() => doWork(id))));
// never more than 2 doWork() calls genuinely running at once
Clarifying questions expected:
- Do queued (not-yet-started) tasks need to run in the order they were submitted?
- Should a task that throws affect the limiter's ability to keep processing the rest of the queue?
- Is there a way to inspect how many tasks are currently active or queued?
Code / implementation expected: Yes — real, direct proof with a live execution log that the number of simultaneously active tasks never exceeds the configured limit.