JavaScript and TypeScript course ยท Module 4: Asynchronous JavaScript

Parallel Execution of Asynchronous Tasks

11 min read ยท
In this lesson13

In Jurassic Park, simultaneously managing many systems and monitoring dozens of dinosaurs requires an efficient approach to running many tasks at the same time. In this exercise we will focus on techniques for parallel execution of asynchronous tasks in JavaScript, which will allow us to increase the performance of our park management systems.

Why Is Parallel Execution Important?

Imagine we need to check the status of all 50 dinosaur enclosures before opening the park. If each check takes about 1 second and we execute them sequentially (one after another), the entire process will take at least 50 seconds - far too long before letting visitors in!

By executing these tasks in parallel, we can significantly reduce the total time needed to perform all operations, limiting it to the duration of the single longest operation (plus some overhead).

Promise.all() - The Basic Tool for Parallel Execution

As we saw in previous lessons, Promise.all() is the fundamental tool for running many asynchronous tasks in parallel:

async function checkAllEnclosureStatuses() {
  const enclosureList = await fetchEnclosureList();
  console.log(`Checking status of ${enclosureList.length} enclosures...`);

  const startTime = Date.now();

  // Launch checking of all enclosures in parallel
  const results = await Promise.all(
    enclosureList.map(enclosure => checkEnclosureStatus(enclosure.id))
  );

  const executionTime = (Date.now() - startTime) / 1000;
  console.log(`Checking all enclosures took ${executionTime.toFixed(2)} seconds`);

  // Analyze results
  const secureEnclosures = results.filter(w => w.status === 'secure');
  const problematicEnclosures = results.filter(w => w.status !== 'secure');

  console.log(`Secure enclosures: ${secureEnclosures.length}`);
  console.log(`Problematic enclosures: ${problematicEnclosures.length}`);

  if (problematicEnclosures.length > 0) {
    console.warn("Problems found in the following enclosures:");
    problematicEnclosures.forEach(w => {
      console.warn(`- Enclosure #${w.id}: ${w.status} (${w.message})`);
    });
  }

  return {
    total: results.length,
    secure: secureEnclosures.length,
    problematic: problematicEnclosures,
    executionTime
  };
}

In the above example, we use Promise.all() to check the status of all enclosures in parallel. Instead of waiting for each check to complete before starting the next, all checks are initiated simultaneously and we wait for all of them to complete.

Challenges of Parallel Execution

Parallel execution of asynchronous tasks brings many benefits, but also comes with certain challenges:

1. Resource Management

Launching too many operations in parallel can overload available system resources (memory, network connections, etc.).

2. Error Handling

Remember that Promise.all() will fail if any one promise is rejected. With a large number of parallel operations, a single error can cause the entire process to fail.

3. Too Much Parallelism

Running all tasks in parallel is not always the best solution. Sometimes we need to control the maximum number of parallel operations.

Controlling Concurrency - Not Too Many at Once

Running too many operations simultaneously can overload the server or API. We need a way to run tasks in parallel but with a limit on how many run at the same time:

async function runWithConcurrencyLimit(tasks, limit) {
  const results = [];
  const executing = [];

  for (const task of tasks) {
    const promise = Promise.resolve().then(() => task());
    results.push(promise);

    if (limit <= tasks.length) {
      const executor = promise.then(() => executing.splice(executing.indexOf(executor), 1));
      executing.push(executor);

      if (executing.length >= limit) {
        await Promise.race(executing);
      }
    }
  }

  return Promise.all(results);
}

// Check all paddocks but maximum 5 at a time
async function checkAllPaddocksLimited() {
  const paddockIds = Array.from({ length: 50 }, (_, i) => i + 1);

  const checkTasks = paddockIds.map(id => () => checkSinglePaddock(id));

  console.time("parallel-limited");
  const results = await runWithConcurrencyLimit(checkTasks, 5);
  console.timeEnd("parallel-limited");

  return results;
}

async function checkSinglePaddock(paddockId) {
  await new Promise(res => setTimeout(res, 200 + Math.random() * 300));
  return { paddockId, status: Math.random() > 0.1 ? "secure" : "alert", checkedAt: new Date().toISOString() };
}

Libraries for Managing Parallelism

Several popular libraries make managing parallel task execution easier:

p-limit

The p-limit library allows easy limiting of parallel operations:

const pLimit = require('p-limit');

async function monitorAllDinosaurs() {
  const dinosaurList = await fetchDinosaurList();

  // Create a limiter instance with at most 5 parallel operations
  const limit = pLimit(5);

  // Create a promise array with limited parallelism
  const promises = dinosaurList.map(dino => {
    return limit(() => monitorDinosaur(dino.id));
  });

  // Wait for all operations to complete
  const results = await Promise.all(promises);

  return results;
}

Parallel Processing with Error Handling

As we mentioned earlier, Promise.all() will fail if any one promise is rejected. In some cases we want to continue processing even if some operations fail.

In ECMAScript 2020, Promise.allSettled() was introduced, which solves this problem:

async function runDiagnosticsForAllSystems() {
  const systems = await fetchAllSystems();

  console.log(`Running diagnostics for ${systems.length} systems...`);

  // Launch diagnostics for all systems in parallel and wait for all to complete
  const results = await Promise.allSettled(
    systems.map(async (system) => {
      try {
        console.log(`Diagnostics for system ${system.name}...`);
        const result = await systemDiagnostics(system.id);
        console.log(`Diagnostics for system ${system.name} completed`);
        return { systemId: system.id, status: 'success', result: result };
      } catch (error) {
        console.error(`Diagnostics error for system ${system.name}:`, error);
        return { systemId: system.id, status: 'error', error: error.message };
      }
    })
  );

  // Analyze results
  const successful = results.filter(w => w.status === 'fulfilled' && w.value.status === 'success');
  const failed = results.filter(w => w.status === 'rejected' || (w.status === 'fulfilled' && w.value.status === 'error'));

  console.log(`Diagnostics completed: ${successful.length} successful, ${failed.length} unsuccessful`);

  return {
    total: results.length,
    successful: successful.length,
    failed: failed.length,
    results
  };
}

Batch Processing

Sometimes it is better to process large collections of items in batches:

async function processDinosaursBatch(dinosaurIds, batchSize = 10) {
  const results = [];

  for (let i = 0; i < dinosaurIds.length; i += batchSize) {
    const batch = dinosaurIds.slice(i, i + batchSize);
    console.log(`Processing batch ${Math.floor(i / batchSize) + 1}: dinosaurs ${i + 1}-${Math.min(i + batchSize, dinosaurIds.length)}`);

    // Process the batch in parallel
    const batchResults = await Promise.all(
      batch.map(id => processSingleDinosaur(id))
    );

    results.push(...batchResults);

    // Optional: small pause between batches to avoid API rate limits
    if (i + batchSize < dinosaurIds.length) {
      await new Promise(res => setTimeout(res, 100));
    }
  }

  return results;
}

async function processSingleDinosaur(id) {
  await new Promise(res => setTimeout(res, 100 + Math.random() * 200));
  return { id, processed: true, health: Math.floor(70 + Math.random() * 30) };
}

// Usage
const allDinoIds = Array.from({ length: 100 }, (_, i) => i + 1);
processDinosaursBatch(allDinoIds, 10).then(results => {
  console.log(`Processed ${results.length} dinosaurs`);
  const avgHealth = results.reduce((sum, d) => sum + d.health, 0) / results.length;
  console.log(`Average health: ${avgHealth.toFixed(1)}%`);
});

Priority Queue

Some operations are more urgent than others. A priority queue ensures critical tasks run first:

class PriorityTaskQueue {
  constructor(concurrency = 3) {
    this.concurrency = concurrency;
    this.running = 0;
    this.queues = { high: [], medium: [], low: [] };
  }

  async add(task, priority = 'medium') {
    return new Promise((resolve, reject) => {
      this.queues[priority].push({ task, resolve, reject });
      this.run();
    });
  }

  run() {
    while (this.running < this.concurrency) {
      const next = this.queues.high.shift() ||
                   this.queues.medium.shift() ||
                   this.queues.low.shift();

      if (!next) break;

      this.running++;
      Promise.resolve()
        .then(() => next.task())
        .then(result => {
          next.resolve(result);
        })
        .catch(error => {
          next.reject(error);
        })
        .finally(() => {
          this.running--;
          this.run();
        });
    }
  }
}

// Usage
const parkTaskQueue = new PriorityTaskQueue(4);

// High priority - security alerts
async function handleSecurityAlert(alertId) {
  return parkTaskQueue.add(
    () => processSecurityAlert(alertId),
    'high'
  );
}

// Medium priority - regular monitoring
async function scheduledMonitoring(paddockId) {
  return parkTaskQueue.add(
    () => checkSinglePaddock(paddockId),
    'medium'
  );
}

// Low priority - data sync
async function syncHistoricalData(dinoId) {
  return parkTaskQueue.add(
    () => syncDinoHistory(dinoId),
    'low'
  );
}

function processSecurityAlert(id) { return new Promise(res => setTimeout(() => res({ alertId: id, handled: true }), 150)); }
function syncDinoHistory(id) { return new Promise(res => setTimeout(() => res({ dinoId: id, synced: true }), 400)); }

Streaming Results

When processing many items, you can stream results as they complete rather than waiting for all:

async function* processWithStreaming(items, processor) {
  const promises = items.map((item, index) =>
    processor(item).then(result => ({ index, result }))
  );

  // Use a tracker to yield results in completion order
  const pending = new Map(promises.map((p, i) => [i, p]));

  while (pending.size > 0) {
    // Wait for the next promise to settle
    const settled = await Promise.race(pending.values());
    pending.delete(settled.index);
    yield settled.result;
  }
}

// Usage - see results as they arrive
async function monitorDinosaursStreaming(dinoIds) {
  const processor = async (id) => {
    await new Promise(res => setTimeout(res, 100 + Math.random() * 900));
    return { id, health: Math.floor(60 + Math.random() * 40), timestamp: Date.now() };
  };

  let count = 0;
  for await (const result of processWithStreaming(dinoIds, processor)) {
    count++;
    console.log(`[${count}/${dinoIds.length}] Dinosaur ${result.id}: ${result.health}% health`);
  }
}

monitorDinosaursStreaming([1, 2, 3, 4, 5, 6, 7, 8]);

Worker Threads in Node.js

In Node.js, we can use the worker_threads module to perform intensive computations in separate threads:

const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');

// Main code
if (isMainThread) {
  async function analyzeDinosaurData() {
    const data = await fetchLargeDataSets();

    // Split data into parts, one for each CPU core
    const threadCount = require('os').cpus().length;
    const dataParts = splitData(data, threadCount);

    // Create a worker for each data part
    const workers = dataParts.map((part, index) => {
      return new Promise((resolve, reject) => {
        const worker = new Worker(__filename, {
          workerData: { data: part, index }
        });

        worker.on('message', resolve);
        worker.on('error', reject);
        worker.on('exit', (code) => {
          if (code !== 0) {
            reject(new Error(`Worker stopped with exit code ${code}`));
          }
        });
      });
    });

    // Wait for all workers to complete
    const results = await Promise.all(workers);

    // Merge results
    return mergeResults(results);
  }
} else {
  // Code executed in each worker thread
  const { data, index } = workerData;

  // Perform complex computations on the data
  const results = processData(data);

  // Send results back to the main thread
  parentPort.postMessage(results);
}

Web Workers for CPU-Intensive Work

For heavy computations that would block the main thread in the browser, use Web Workers:

// main.js - creating a Web Worker
function analyzeDinoPopulationStats(dinosaurData) {
  return new Promise((resolve, reject) => {
    const worker = new Worker('dino-stats-worker.js');

    worker.postMessage({ type: 'ANALYZE', data: dinosaurData });

    worker.onmessage = (event) => {
      if (event.data.type === 'RESULT') {
        resolve(event.data.result);
        worker.terminate();
      }
    };

    worker.onerror = (error) => {
      reject(error);
      worker.terminate();
    };
  });
}

// dino-stats-worker.js - runs in separate thread
self.onmessage = function(event) {
  if (event.data.type === 'ANALYZE') {
    const { data } = event.data;

    // CPU-intensive computation - does not block the main thread
    const stats = {
      totalWeight: data.reduce((sum, d) => sum + d.weight, 0),
      avgAge: data.reduce((sum, d) => sum + d.age, 0) / data.length,
      speciesDistribution: data.reduce((acc, d) => {
        acc[d.species] = (acc[d.species] || 0) + 1;
        return acc;
      }, {}),
      dangerIndex: data.reduce((sum, d) => sum + d.dangerLevel, 0) / data.length
    };

    self.postMessage({ type: 'RESULT', result: stats });
  }
};

Adaptive Task Processor

A complete solution that adapts concurrency based on system load:

class AdaptiveTaskProcessor {
  constructor(options = {}) {
    this.minConcurrency = options.minConcurrency || 1;
    this.maxConcurrency = options.maxConcurrency || 20;
    this.currentConcurrency = options.initialConcurrency || 5;
    this.running = 0;
    this.queue = [];
    this.successCount = 0;
    this.errorCount = 0;
    this.adjustInterval = setInterval(() => this.adjustConcurrency(), 5000);
  }

  adjustConcurrency() {
    const errorRate = this.errorCount / (this.successCount + this.errorCount || 1);

    if (errorRate > 0.1 && this.currentConcurrency > this.minConcurrency) {
      this.currentConcurrency = Math.max(this.minConcurrency, this.currentConcurrency - 2);
      console.log(`High error rate (${(errorRate * 100).toFixed(0)}%) - reducing concurrency to ${this.currentConcurrency}`);
    } else if (errorRate < 0.02 && this.currentConcurrency < this.maxConcurrency) {
      this.currentConcurrency = Math.min(this.maxConcurrency, this.currentConcurrency + 1);
      console.log(`Low error rate - increasing concurrency to ${this.currentConcurrency}`);
    }

    // Reset counters
    this.successCount = 0;
    this.errorCount = 0;
    this.process();
  }

  async add(task) {
    return new Promise((resolve, reject) => {
      this.queue.push({ task, resolve, reject });
      this.process();
    });
  }

  process() {
    while (this.running < this.currentConcurrency && this.queue.length > 0) {
      const { task, resolve, reject } = this.queue.shift();
      this.running++;

      Promise.resolve()
        .then(() => task())
        .then(result => {
          this.successCount++;
          resolve(result);
        })
        .catch(error => {
          this.errorCount++;
          reject(error);
        })
        .finally(() => {
          this.running--;
          this.process();
        });
    }
  }

  destroy() {
    clearInterval(this.adjustInterval);
  }
}

// Usage
const processor = new AdaptiveTaskProcessor({ minConcurrency: 2, maxConcurrency: 15, initialConcurrency: 5 });

const dinoIds = Array.from({ length: 200 }, (_, i) => i + 1);
const tasks = dinoIds.map(id => () => processSingleDinosaur(id));

Promise.allSettled(tasks.map(t => processor.add(t))).then(results => {
  const successful = results.filter(r => r.status === 'fulfilled').length;
  console.log(`Processed ${successful}/${results.length} dinosaurs successfully`);
  processor.destroy();
});

Summary

Parallel execution of asynchronous tasks is a key tool for optimizing JavaScript application performance. In Jurassic Park, where monitoring and managing multiple systems and dinosaurs requires fast responses, proper use of parallel processing techniques can significantly improve the responsiveness and scalability of the system.

We learned several important techniques and patterns:

  1. Promise.all() - the fundamental tool for running many asynchronous tasks in parallel
  2. Controlling the number of parallel operations - techniques for limiting maximum concurrency
  3. Batch processing - processing data in batches to manage memory and API limits
  4. Priority task queues - executing more important tasks first
  5. Stream processing - processing data as it arrives for responsive UI
  6. Worker Threads and Web Workers - executing tasks in separate threads for CPU-intensive work
  7. Adaptive resource management - dynamically adjusting the level of parallelism based on observed error rates

The choice of the right technique depends on the specific use case, performance requirements, and available resources. Remember that more parallelism does not always mean better performance - too many parallel operations can lead to system overload and, consequently, to degraded overall performance.

In practice, the best results are often achieved by experimenting with different strategies and parallelism levels, monitoring performance, and adjusting parameters accordingly.

Code for this lesson: index.js
// Hoisting Demonstration - Lifting declarations
console.log("HOISTING IN JAVASCRIPT\n");

console.log("=== VARIABLE HOISTING ===\n");

// var is hoisted
console.log("1. dinosaurName before declaration:", dinosaurName); // undefined (not an error!)
var dinosaurName = "T-Rex";
console.log("2. dinosaurName after declaration:", dinosaurName); // T-Rex

// The above code is interpreted as:
// var dinosaurName; // declaration hoisted to the top
// console.log(dinosaurName); // undefined
// dinosaurName = "T-Rex"; // assignment

console.log("\n=== HOISTING let/const ===\n");

// let and const are in the "temporal dead zone"
try {
  console.log(species); // ReferenceError!
  let species = "Velociraptor";
} catch (error) {
  console.log("Error:", error.message);
}

console.log("\n=== FUNCTION HOISTING ===\n");

// Function declaration is fully hoisted
console.log("Call before declaration:");
feedDinosaur("T-Rex"); // Works!

function feedDinosaur(name) {
  console.log(`Feeding ${name}`);
}

console.log("\nCall after declaration:");
feedDinosaur("Triceratops");

console.log("\n=== FUNCTION EXPRESSION ===\n");

// Function expression is NOT hoisted
try {
  checkSecurity("Sector A"); // TypeError!
} catch (error) {
  console.log("Error:", error.message);
}

var checkSecurity = function(sector) {
  console.log(`Checking ${sector}`);
};

// Now works
checkSecurity("Sector B");

console.log("\n=== PRACTICAL EXAMPLE ===\n");

function demonstrateHoisting() {
  console.log("Function starts");
  console.log("Value of x:", x); // undefined

  if (true) {
    var x = "I am var - function scoped";
    let y = "I am let - block scoped";
    const z = "I am const - block scoped";

    console.log("In the block - x:", x);
    console.log("In the block - y:", y);
    console.log("In the block - z:", z);
  }

  console.log("After block - x:", x); // Works!
  // console.log("After block - y:", y); // ERROR! y is block scoped
  // console.log("After block - z:", z); // ERROR! z is block scoped
}

demonstrateHoisting();

console.log("\n=== BEST PRACTICES ===");
console.log("Use const/let instead of var");
console.log("Declare variables at the beginning of scope");
console.log("Use function declarations for global functions");
console.log("Use arrow functions for local functions");

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. Why is Promise.all() faster than sequential await for independent operations?

  2. 2. What are Worker Threads used for in Node.js?

These are 2 of 3 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Code editor

    Implement a park event system using callbacks - create functions that accept callbacks for different scenarios (success/error).

  • Vertical ordering

    Arrange the elements of an async function with await:

  1. QuestionPromise.all and Parallelism
  2. QuestionWorker Threads in Node.js
  3. QuestionError Handling in Promise.all
  4. Coding taskPractice: Asynchronous Callbacks
  5. Put in orderasync/await Element Order

Useful articles