JAVASCRIPT

Execute Concurrent API Requests with a Limited Pool

Efficiently run multiple API requests concurrently while respecting concurrency limits using a custom Promise-based pool, preventing API overload.

async function runWithConcurrencyLimit(tasks, limit) {
  const results = new Array(tasks.length);
  const runningPromises = [];
  let taskIndex = 0;

  const executeTask = async (taskFn, index) => {
    try {
      const result = await taskFn();
      results[index] = { status: 'fulfilled', value: result };
    } catch (reason) {
      results[index] = { status: 'rejected', reason: reason };
    }
  };

  return new Promise(resolve => {
    const processNext = () => {
      if (taskIndex >= tasks.length && runningPromises.length === 0) {
        return resolve(results);
      }

      while (taskIndex < tasks.length && runningPromises.length < limit) {
        const currentTaskIndex = taskIndex;
        const promise = executeTask(tasks[currentTaskIndex], currentTaskIndex);
        runningPromises.push(promise);
        promise.finally(() => {
          const idx = runningPromises.indexOf(promise);
          if (idx > -1) {
            runningPromises.splice(idx, 1);
          }
          processNext(); // Process next available task
        });
        taskIndex++;
      }
    };

    processNext();
  });
}

// Example usage:
const apiCalls = [
  () => fetch('https://jsonplaceholder.typicode.com/todos/1').then(res => res.json()),
  () => fetch('https://jsonplaceholder.typicode.com/posts/1').then(res => res.json()),
  () => fetch('https://jsonplaceholder.typicode.com/users/1').then(res => res.json()),
  () => fetch('https://jsonplaceholder.typicode.com/comments/1').then(res => res.json()),
  () => fetch('https://jsonplaceholder.typicode.com/albums/1').then(res => res.json()),
];

runWithConcurrencyLimit(apiCalls, 2) // Run 2 API calls at a time
  .then(allResults => {
    console.log('All API calls completed (results in order):', allResults);
  })
  .catch(error => console.error('Error during concurrent execution:', error));
How it works: This JavaScript snippet provides a powerful utility to execute multiple asynchronous API requests concurrently, but with a configurable concurrency limit. Unlike a simple `Promise.all` which runs all promises simultaneously, this function maintains a maximum number of active requests at any given time. This is crucial for preventing API rate limits, conserving resources, or simply managing network load when dealing with a large batch of independent API calls, ensuring a more stable and predictable integration.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs