@arthur.lee945/promise-pool
v1.0.2
Published
Queue based promise pool
Downloads
3
Maintainers
Readme
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();