@jacoblincool/puddle
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

puddle

Pooling? Maybe.

Usage

import Pool from "@jacoblincool/puddle";
// const Pool = require("@jacoblincool/puddle");

// An example task
async function task(id) {
    await new Promise((resolve) => setTimeout(resolve, 500));
    return id;
}

// Create a pool with a maximum of 4 tasks
const pool = new Pool(4);

// Add tasks to the pool
for (let i = 0; i < 20; i++) {
    pool.push(() => task(i + 1));
}

// Subscribe to the pool's events
pool.on("task-start", (idx) => {
    console.log(`Task ${idx} started`);
});
pool.on("task-finish", (idx, result) => {
    console.log(`Task ${idx} ended with result ${result}`);
});

// Run the tasks
const results = await pool.go();
console.log(results);

Readme

Keywords

Package Sidebar

Install

npm i @jacoblincool/puddle

Weekly Downloads

93

Version

0.1.1

License

MIT

Unpacked Size

7.52 kB

Total Files

6

Last publish

Collaborators

  • jacoblincool