A pure JavaScript module with no dependencies, that aims to parallelly run promise list with max concurrency by a run queue.
- No dependencies
- Support NodeJS and Browser
- Support
progress
,finished
event
Using npm:
npm install promise-queue-runner
Using yarn:
yarn add promise-queue-runner
In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with require() use the following approach:
const Runner = require('promise-queue-runner').default;
// or
const { Runner } = require('promise-queue-runner');
import { Runner } from 'promise-queue-runner';
// or
import Runner from 'promise-queue-runner';
const promiseGenerators = [];
for (let i = 0; i < 100; i += 1) {
promiseGenerators.push(
() => new Promise(
(resolve, reject) => {
// do something
resolve('value');
}
)
);
}
const runner = new Runner({
promiseGenerators,
maxConcurrency: 5
});
runner.addListener(
Runner.PROGRESS,
/**
* @param {Number} finish - count of promise that has been run current
* @param {Object} latestResult - the result of latest promise that has been run
* @param {Object} results - the results of all promise that has been run
*/
({ finish, latestResult, results }) => {
console.log(latestRusult);
console.log(results);
// results example
// {
// 0: { error: null, value: 'value1' },
// 1: { error: null, value: 'value2' }
// }
console.log(`progress: ${finish} / ${promiseGenerators.length}`);
}
);
runner.addListener(
Runner.FINISHED,
(results) => {
// `results` is same as the results of `PROGRESS` event callback
console.log(results);
}
);
runner.start();