@bul/pool
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Pool

npm version Coverage Status Build Status

When we have too much tasks in parallel, pool is simple way to queue & throttle tasks efficiently.

Usage

Example: We have a lot urls to fetch data, doing ALL in parallel may hang our computer

Steps:

  1. Decide pool's size. Ex: 5
  2. Push async task & args into pool
  3. Wait for pool idle & get result
import Pool from '@bul/pool';
import fetch from 'isomorphic-fetch';

const runDemo = async () => {
  // Simple async task
  const demoAsyncTask = url =>
    fetch(url)
      .then(res => res.text())
      .catch(console.log);

  const urls = [
    'https://google.com', //
    'https://medium.com',
    'https://github.com',
    /* other urls */
  ];

  // 1. Create pool
  const pool = Pool(5);

  // 2. Push async task into pool
  urls.map(url => pool.push(demoAsyncTask, url /* Pass ALL async tasks' args here */));

  // 3.1 Wait for pool idle
  await pool.idle();

  // 3.2 Get result after all async tasks done
  return pool.getResult();
};

runDemo()
  .then(console.log)
  .catch(console.log);

Readme

Keywords

none

Package Sidebar

Install

npm i @bul/pool

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

20.4 kB

Total Files

24

Last publish

Collaborators

  • hoanganh25991