0Pricing
JavaScript Academy · Lesson

E3 — Promise Pool/Queue (async concurrency)

Run many async tasks with a small concurrency limit. Keep a queue, start a few at a time, and collect results.

What and why

Goal: Run many async jobs without flooding the system.

  • Limit concurrency (e.g., 2 at a time)
  • Keep a simple queue index
  • Store results in an array
  • Beginner-friendly code, small helpers
E3 — Promise Pool/Queue (async concurrency) — illustration 1

Async job helper

A tiny helper returns a Promise that resolves after a delay; optionally fails for testing.

// Simulate async work with setTimeout
function fakeJob(name, ms, fail) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      if (fail) {
        reject(new Error("fail:" + name));
      } else {
        resolve("done:" + name);
      }
    }, ms);
  });
}

console.log("demo job:", typeof fakeJob === "function");
E3 — Promise Pool/Queue (async concurrency) — illustration 2

All lessons in this course

  1. E1 — Expression Evaluator (part 1: + and - LTR)
  2. E2 — Log Analyzer (simple filters & counters)
  3. E3 — Promise Pool/Queue (async concurrency)
← Back to JavaScript Academy