Skip to content

Bounded Concurrency

The Course
•
25 min study

Promise.all coordinates promises that have already been started; it does not impose a limit. Mapping ten thousand inputs to requests can start ten thousand operations before awaiting any. A concurrency limit must control when the mapper is called.

One simple implementation processes batches. Each batch starts at most limit jobs, waits for all of them, then starts the next batch. This is easy to reason about but leaves capacity unused when one job in a batch is much slower than its peers. A worker-pool design refills an available slot immediately; Day 7 supplies that complete implementation.

Concurrency overlaps waiting. It does not make synchronous CPU work run in parallel on the JavaScript thread. The limit should be chosen for the resource being protected, not from a claim that a larger number is always faster. The active-call count excludes jobs that have already completed.

Predict, run, and explain

Write the output and trace the important state changes before checking it. Then run the program. Each editor is independent. Async examples end with main() so this editor waits for their work.

Predict, then run JavaScript
async function main() {
  let active = 0;
  let peak = 0;

  async function task(value) {
    active++;
    peak = Math.max(peak, active);
    await Promise.resolve();
    active--;
    return value * 2;
  }

  const results = [];

  for (let start = 0; start < 5; start += 2) {
    const group = [1, 2, 3, 4, 5].slice(start, start + 2);

    results.push(...(await Promise.all(group.map(task))));
  }

  console.log(results.join(","));
  console.log(peak);
}

main();
Check your prediction
Text
2,4,6,8,10
2

Measure active work rather than counting promises

Concurrency bounds are about operations that have started and not settled. Merely creating an array with two elements does not prove at most two callbacks are active throughout a larger run. Observe starts, completions and peak activity through the mapper boundary.

A batch leaves capacity idle while its slowest member finishes. A worker loop can refill a free slot immediately. Both can preserve result order by reserving positions before awaiting; neither makes synchronous CPU work parallel on the same JavaScript thread.

Measure active work rather than counting promises: trace JavaScript
async function main() {
  let active = 0;
  let peak = 0;

  async function work(n) {
    active++;
    peak = Math.max(peak, active);
    await Promise.resolve();
    active--;
    return n;
  }

  await work(1);
  await work(2);
  console.log(peak);
  peak = 0;
  await Promise.all([work(1), work(2)]);
  console.log(peak);
}

main();
Check your prediction
Text
1
2

The same mapper is used in both schedules. Sequential await prevents overlap; calling both before awaiting permits it. Peak counts active invocations, not array length after completion.

Measure active work rather than counting promises: contrast JavaScript
async function main() {
  const results = [];
  const first = Promise.resolve("A");
  const second = Promise.resolve("B");

  await Promise.all([
    second.then(value => {
      results[1] = value;
    }),
    first.then(value => {
      results[0] = value;
    })
  ]);
  console.log(results.join(","));
}

main();
Check your prediction
Text
A,B

Each callback writes to the input’s reserved position, independent of its registration order. A later controlled schedule can reverse completion without changing the output contract.

Try the rule on new inputs

Compare limits one and two using active and peak counters. Then hold one callback on a deferred promise and identify the event that proves another worker slot was refilled before releasing it.

Debug a plausible mistake

Exercise

Repair result order under overlap

Retain input order even when the second job completes first. Print first,second. Keep the controlled completion schedule; changing its order would hide the defect rather than repair the report.

Predict the supplied program's actual behavior first. It runs without an error, but breaks the requirement. Make a small repair, then try a case that would distinguish the repaired version from the original.

Debugging workspace JavaScript
async function main() {
  let finishFirst;
  const first = new Promise(resolve => {
    finishFirst = resolve;
  });
  const results = [];
  const firstWork = first.then(value => {
    results.push(value);
  });
  const secondWork = Promise.resolve("second").then(value => {
    results.push(value);
  });
  await secondWork;
  finishFirst("first");
  await firstWork;
  console.log(results.join(","));
}

main();
Hint 1: the idea

Completion order and input position are different properties.

Hint 2: a first step

Assign a stable result position to each job before awaiting completion.

Show one solution
JavaScript
async function main() {
  let finishFirst;
  const first = new Promise(resolve => {
    finishFirst = resolve;
  });
  const results = [];
  const firstWork = first.then(value => {
    results[0] = value;
  });
  const secondWork = Promise.resolve("second").then(value => {
    results[1] = value;
  });
  await secondWork;
  finishFirst("first");
  await firstWork;
  console.log(results.join(","));
}

main();
Text
first,second

The original printed second,first. push records the order in which jobs finish, and the schedule deliberately finishes the second job first. Writing to results[0] and results[1] ties each result to the position of its input, so completion order no longer matters. A test that released the jobs in input order would pass with either version; only an out-of-order schedule like this one tells them apart.

Trace the contract before building

Count mapper invocations that have started but not settled. A limit controls that active count, not merely the number of promises passed to Promise.all afterward. Starting all requests and then grouping their promises has already spent the concurrency budget.

Use a controlled slow first job and a quick second job to contrast batches with refilled worker slots. A worker pool can use the freed slot without waiting for the slow peer. Concurrency overlaps waiting on the JavaScript thread; synchronous heavy work still needs its own responsiveness strategy. Test start count and result order independently.

Your turn

Exercise

Bounded Concurrency: practice

Write batchMap(values, mapper, limit) that validates a positive integer limit and processes one group at a time with Promise.all. For [1,2,3] and an async doubler at limit 2, print 2,4,6.

Your workspace JavaScript
// Use async main() and call it last; define the batch helper inside main.
Hint 1: the idea

Do not invoke the mapper for the next group before the current group settles.

Hint 2: a first step

Slice the input from start to start + limit, then await that group.

Show one solution
JavaScript
async function main() {
  async function batchMap(values, mapper, limit) {
    if (!Number.isInteger(limit) || limit < 1) {
      throw new RangeError("Invalid limit");
    }

    const result = [];

    for (let start = 0; start < values.length; start += limit) {
      result.push(
        ...(await Promise.all(values.slice(start, start + limit).map(mapper)))
      );
    }

    return result;
  }

  console.log(
    (await batchMap([1, 2, 3], async value => value * 2, 2)).join(",")
  );
}

main();
Text
2,4,6

Promise.all on one slice at a time is what enforces the limit: the await stops the loop until the whole group has settled, so the mapper is never called for item 3 while items 1 and 2 are running. Promise.all also returns the group's results in input order, and spreading them onto result keeps the overall order. The cost is idle capacity: if item 1 is slow and item 2 is fast, the second slot waits for item 1 instead of taking item 3. That is the gap the worker loop above closes.

From batches to worker slots

Before Day 7, learn the core loop without failure or cancellation policies. Here a worker is an async function, not a Web Worker or another thread. Two calls share one next counter. Each reserves its index synchronously, then pauses at await. When a call resumes, it can reserve another item without waiting for the other slot.

Predict, then run JavaScript
async function main() {
  async function pool(values, mapper, limit) {
    let next = 0;
    const results = [];

    async function worker() {
      while (next < values.length) {
        const index = next++;

        results[index] = await mapper(values[index]);
      }
    }

    const workers = [];

    for (let slot = 0; slot < limit; slot++) {
      workers.push(worker());
    }

    await Promise.all(workers);
    return results;
  }

  console.log((await pool([3, 1, 2], async n => n * 10, 2)).join(","));
}

main();
Check your prediction
Text
30,10,20

Trace which part runs before the first await. Reserving the index afterward would let two calls choose the same item. Assigning by index preserves input order even when completion order differs.

The capstone writes the worker-start loop as Array.from({ length: limit }, () => worker()): the first argument requests that many positions, and the callback starts one worker per position. It also replaces array indexing with the iterator protocol from Day 2. This small version assumes valid inputs and successful callbacks. If one rejects, Promise.all rejects early while other work may continue; Day 7 adds the explicit stop-and-settle policy. Day 5 teaches how to test these schedules without timing guesses.

Change it and test it

Test empty input and limit 1. Add an active counter and assert its peak stays within the limit. Explain the slow-batch limitation and why this toy does not yet promise peer cancellation or waiting for peers before rejecting.

Was this article helpful?

Your response is saved on this device.