Problem, as an interviewer would state it:
"Build a TaskQueue class with an add(task) method and a configurable concurrency limit. Unlike a bare function wrapper, I want to be able to inspect how many tasks are currently running and how many are waiting."
Examples:
const q = new TaskQueue(2);
q.add(() => doWork(1));
q.add(() => doWork(2));
q.add(() => doWork(3)); // waits, since 2 are already running
console.log(q.running, q.pending); // 2, 1
Clarifying questions expected:
- Is this genuinely different in real capability from a plain
p-limit-style function, or mainly a different API shape? - Should
running/pendingbe live-readable properties, or only available via a method call? - Does
add()need to return a promise the caller canawaitfor that specific task's own result?
Code / implementation expected: Yes — real, direct, live proof that immediately after enqueueing tasks beyond the concurrency limit, the object's own running/pending state correctly reports the real, current numbers.