Problem, as an interviewer would state it:
"Implement mapLimit(items, limit, mapper) — like Array.prototype.map, but mapper is async, and no more than limit calls should ever run concurrently, while the FINAL results array still preserves the original input order."
Examples:
await mapLimit([1,2,3,4,5], 2, async (n) => n * 10); // [10,20,30,40,50], never more than 2 running at once
Clarifying questions expected:
- Must the RESULTS array preserve original input order, even though the underlying calls may COMPLETE out of order?
- Should one item's mapper rejecting stop the whole operation immediately, or let already-started items finish first?
- Is this conceptually the same underlying mechanism as this bank own p-limit question?
Code / implementation expected: Yes — real, direct proof via a logged execution trace that concurrency stays capped at the limit, and that results preserve original order despite items completing at different times.