#8MediumJavaScript functions
8. mapAsyncLimit
JavaScriptAsync & Promises
Implement mapAsyncLimit(iterable, callbackFn, size) to run an async mapper with at most `size` concurrent operations.
Example 1:
await mapAsyncLimit([1, 2, 3, 4], async (n) => n * 2, 2);
// [2, 4, 6, 8]solution.js
Maps items in order while respecting concurrency limit
(async () => {
let active = 0;
let maxActive = 0;
const res = await mapAsyncLimit([1, 2, 3, 4], async (n) => {
active++;
maxActive = Math.max(maxActive, active);
await new Promise((r) => setTimeout(r, 10));
active--;
return n * 2;
}, 2);
expect(res).toEqual([2, 4, 6, 8]);
expect(maxActive <= 2).toBe(true);
})()