Nominating Presidential Muppets
    Wondering what’s next for npm?Check out our public roadmap! »

    promise-allsettled-pool
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.3 • Public • Published

    promise-allsettled-pool

    what

    Use an API like Promise.allSettled to manage the promise pool;

    Install

    npm

    $ npm install promise-allsettled-pool
    

    yarn

    $ yarn add promise-allsettled-pool
    

    Usage

    import PromisePool from 'promise-allsettled-pool';
    // node.js
    // const PromisePool = require('promise-allsettled-pool').default;
    const promiseIteratorList = [
        () => new Promise((res) => {
            setTimeout(() => {
                res(0)
            }, 10)
        }),
        () => Promise.resolve(1),
        () => Promise.reject(2),
        () => new Promise((_, rej) => {
            setTimeout(() => {
                rej(3)
            }, 20)
        }),
    ]; 
    const pool = new PromisePool({
        promiseIteratorList,
        thread: 1,
    });
    pool.onEveryDone((res, resList) => {
        console.log(res, resList)
    })
    pool.onDone((resultList) => {
        console.log(resultList)
    })
    pool.pause()
    setTimeout(() => {
        console.log(pool.resultList)
        pool.resume();
    }, 50)
    pool.then((resList) => {
        expect(resList).toEqual(resultList);
    });

    API

    new PromisePool(opt:PromisePoolInitOpt);

    PromisePoolInitOpt

    {
      /** Number of simultaneous concurrent. default:2 */
      thread?: number,
      /** promise iterator list */
      promiseIteratorList: PromiseIterator[],
      /** Whether to store the result of a promise return.default:true */
      saveData?: boolean,
    }

    .then((resultList:Result[]) => any)

    Listening queue all completed. Just a Promise

    Result

    {
      status:Status,
      value?:any,
      reason?:any,
    }
    Status

    { pending= 'pending', fulfilled='fulfilled', rejected='rejected', }

    .pause()

    Suspension follow-up

    .resume()

    Resuming from a pause

    .onDone((resultList:Result[]) => void)

    Listening queue all completed

    .onEveryDone((everyDoneListener: EveryDoneListener) => void)

    Listen to every completion.

    Install

    npm i promise-allsettled-pool

    DownloadsWeekly Downloads

    1

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    41.1 kB

    Total Files

    21

    Last publish

    Collaborators

    • avatar