Language concepts

Concurrency limit

Try the free JavaScript compiler

Application policy limiting active operations. Preserve input ordering and define how failures and cancellation affect queued and running work.

Formal Syntax & Structure

javascript syntaxjavascript
await mapLimit(items, limit, task);

Working Example

Example Snippetjavascript
async function mapLimit(items, limit, fn) {  if (!Number.isInteger(limit) || limit < 1) throw new RangeError("limit");  let next = 0; const out = [];  async function worker() { while (next < items.length) { const i = next++; out[i] = await fn(items[i]); } }  await Promise.all(Array.from({length:Math.min(limit,items.length)},worker)); return out;}let active=0, peak=0;mapLimit([1,2,3,4],2,async n=>{active++; peak=Math.max(peak,active); await 0; active--; return n*2;}).then(r=>console.log(r.join(","),peak));
JavaScript Reference