promise-allsettled-pool
v1.0.3
Published
promise pool like Promise.allSettled
Downloads
1
Readme
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.