@arthur.lee945/promise-pool
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Promise Pool

Queue based promise pool for node.js.

Leave a Like! or check out my portfolio arthurlee.bio

Latest Version Monthly downloads

Usage

Using this Promise pool is relatively simple.

Here’s an example using a concurrency of 2:

import { PromisePool } from 'promise-pool';

const Tasks = [() => new Promise((resolve) => resolve('Resolving Promise'))];

const result = new PromisePool(Tasks).process();

The promise pool takes in this options

new PromisePool(SampleTest, {
    concurrency:10;
    settle: true;
    stream?: (returns) => {
        //DoSomething
    };
})

Useful Methods

  • enqueue

    /**
     * Add a promise to the queue
     * @param { ProcessableItem<TReturn> } items (..._args: any[]) => Promise<TReturn>
     * @returns {PromisePool}
     */
    const processOnQueue = true;
    new PromisePool().enqueue(tasks, processOnQueue);
  • dequeue

    /**
     * Take out next promise in queue or undefined
     * @returns { ProcessableItem<TReturn>[] | undefined }
     */
    new PromisePool().dequeue();
  • peek

    /**
     * Take a peek at next promise to be processed in queue
     * @returns { ProcessableItem<TReturn> | undefined }
     */
    new PromisePool().peek();
  • isEmpty

    /**
     * Check if there are processable items in queue
     * @returns { boolean }
     */
    new PromisePool().isEmpty();
  • getTaskMeta

    /**
     * Take a peek at next promise to be processed in queue
     * @param { "updated" | "stopped" | "isProcessing" } opt
     * @returns { boolean }
     */
    new PromisePool().getTaskMeta('isProcessing');
  • setConcurrency

    /**
     * @param { number } concurrency number to update
     * @returns { PromisePool<TReturn> }
     */
    new PromisePool().setConcurrency(100);
  • setStream

    /**
     * @param { (_data: TReturn[]) => void } cb  Add or Update stream callback function
     * @returns { PromisePool<TReturn> }
     */
    new PromisePool().setStream((returns) => {
        //Do something
    });
  • stop

    /**
     * Stops Currently running process excluding already processing task set
     * @returns {Promise<(TReturn | PromiseSettledResult<TReturn>)[]>}
     */
  • process

    /**
     * Starts the process
     * @returns {Promise<(TReturn | PromiseSettledResult<TReturn>)[]>}
     */
    new PromisePool().process();

Package Sidebar

Install

npm i @arthur.lee945/promise-pool

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

28.2 kB

Total Files

17

Last publish

Collaborators

  • arthur.lee945