Get started with es6-promise-pool CDN

MIT licensed

Es6-Promise-Pool: Manages & executes multiple concurrent/sequential Promises, limiting concurrent tasks.

Tags:
  • promise
  • promises
  • promises-a
  • promises-aplus
  • future
  • futures
  • deferred
  • deferreds
  • generator
  • generators
  • async
  • await
  • flow control
  • pool
  • queue
  • throttle
  • es6
  • browser
  • node

Stable version

Copied!

How to start using es6-promise-pool CDN


// Include the library using a script tag
const PromisePool = require('es6-promise-pool').default;

// Define a function that returns a promise
const fetchData = url =>
  new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.onload = () => resolve(xhr.responseText);
    xhr.onerror = () => reject(new Error(`Error fetching from ${url}`));
    xhr.send();
  });

// Create a promise pool with a size of 3
const pool = new PromisePool(3, fetchData);

// Add tasks to the pool
const tasks = [
  'https://jsonplaceholder.typicode.com/todos/1',
  'https://jsonplaceholder.typicode.com/todos/2',
  'https://jsonplaceholder.typicode.com/todos/3',
  'https://jsonplaceholder.typicode.com/todos/4' // More tasks than the pool size will be queued
];

// Run all tasks in parallel using the pool
pool.run(tasks)
  .then(values => Promise.all(values)) // Wait for all tasks to complete and get their results
  .then(results => console.log(results)) // Log the results
  .catch(error => console.error(error)); // Log any errors
Copied!
Copied!
Copied!

All versions