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.

E3 — Promise Pool/Queue (async concurrency) is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Pool implementation

runPool starts a few workers; each pulls the next task index and awaits it. Results keep input order.

// Run tasks with a fixed concurrency limit
async function runPool(tasks, limit) {
  // tasks: array of functions () => Promise<any>
  const results = new Array(tasks.length);
  let next = 0;

  // worker runs tasks until none left
  async function worker() {
    while (true) {
      const i = next;
      if (i >= tasks.length) return;
      next = next + 1;

      try {
        const value = await tasks[i]();
        results[i] = { ok: true, value: value };
      } catch (e) {
        results[i] = { ok: false, error: String(e) };
      }
    }
  }

  // start up to limit workers
  const workers = [];
  for (let k = 0; k < limit; k = k + 1) {
    workers.push(worker());
  }

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

console.log("pool ready:", typeof runPool === "function");
E3 — Promise Pool/Queue (async concurrency) — illustration 3

Run the pool

Limit to 2 in-flight jobs. The array keeps order: index i holds result of task i.

// Prepare simple tasks
const jobs = [
  function () { return fakeJob("A", 300, false); },
  function () { return fakeJob("B", 200, false); },
  function () { return fakeJob("C", 150, true ); },
  function () { return fakeJob("D", 100, false); }
];

// Run with concurrency = 2
(async function () {
  const out = await runPool(jobs, 2);
  console.log("results:", out);
})();
E3 — Promise Pool/Queue (async concurrency) — illustration 4

Optional retry wrapper

Wrap a task with a tiny withRetry helper. Keep numbers small and logic simple.

// Optional: add retry to a task wrapper (beginner-friendly)
function withRetry(task, attempts) {
  return async function () {
    let lastErr = null;
    for (let i = 0; i < attempts; i = i + 1) {
      try {
        return await task();
      } catch (e) {
        lastErr = e;
      }
    }
    throw lastErr;
  };
}

const jobsWithRetry = [
  withRetry(function () { return fakeJob("R1", 80, true); }, 2),
  withRetry(function () { return fakeJob("R2", 60, false); }, 2)
];

(async function () {
  const out = await runPool(jobsWithRetry, 1);
  console.log("retry results:", out);
})();
E3 — Promise Pool/Queue (async concurrency) — illustration 5

Beginner guidance

Tips:

  • Store tasks as () => Promise functions.
  • Keep a shared next index for the queue.
  • Use try/catch per task and store results.
  • Pick a small limit (2–4) for beginners.
E3 — Promise Pool/Queue (async concurrency) — illustration 6

Concurrency limit basics quiz

Quick check: Why a pool?

E3 — Promise Pool/Queue (async concurrency) — illustration 7

Recap

Recap: A tiny promise pool runs tasks with a fixed limit, keeps order in a results array, and uses simple try/catch for each task. Great beginner pattern.

E3 — Promise Pool/Queue (async concurrency) — illustration 8

Frequently asked questions

Is the “E3 — Promise Pool/Queue (async concurrency)” lesson free?

Yes — the full text of “E3 — Promise Pool/Queue (async concurrency)” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “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. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “E3 — Promise Pool/Queue (async concurrency)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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