1. Concept library
  2. Backend with Node.js
  3. Module 4: Async patterns

Concept

Concurrency limits

Starting a thousand requests or file reads at once exhausts sockets, file descriptors or the downstream service. Run them in batches, or with a limiter that keeps a fixed number in flight.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what runs out when a thousand requests start at once
  2. Process a list of jobs with a fixed number in flight
  3. Spot the for loop of awaits that gives no parallelism

In practice

A few lines that show Concurrency limits doing its job.

JavaScript
async function mapLimit(items, limit, fn) {
  const results = new Array(items.length);
  let next = 0;
  async function worker() {
    while (next < items.length) {
      const i = next++;
      results[i] = await fn(items[i]);
    }
  }
  await Promise.all(Array.from({ length: limit }, worker));
  return results;
}

const pages = await mapLimit(urls, 5, fetchPage);

The common mistake

Watch for

Replacing Promise.all with a for loop of awaits to "be safe", which processes one item at a time. A limiter gives you bounded parallelism instead of none.

What to know first

Your roadmap puts these before Concurrency limits, unless you already know them.

How it stays learned

Once a session shows you understand Concurrency limits, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Error propagation