Limiting concurrency in JavaScript
A pool of workers pulling from a shared queue of tasks.
async function mapLimit(items, limit, worker) {
const results = new Array(items.length);
let next = 0;
async function run() {
while (next < items.length) {
const index = next;
next += 1;
results[index] = await worker(items[index], index);
}
}
await Promise.all(Array.from({ length: limit }, run));
return results;
}
async function main() {
const doubled = await mapLimit([1, 2, 3, 4, 5], 2, async (n) => {
await new Promise((resolve) => setTimeout(resolve, 5));
return n * 2;
});
console.log(doubled);
}
main();
How it works
- Each worker loops until the queue is empty.
- The limit is how many workers you start.
- Results go back by index, so the order is preserved.
Keywords and builtins used here
ArrayPromiseasyncawaitconstfromfunctionletreturnwhile
The run, in numbers
- Lines
- 25
- Characters to type
- 515
- Tokens
- 160
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 87 seconds.
Step 2 of 4 in Canceling & limiting, step 16 of 19 in Promises & async.