UNPKG

2.96 kBMarkdownView Raw
1# promise-queue-manager
2
3[![npm version](https://img.shields.io/npm/v/promise-queue-manager.svg?style=flat)](https://www.npmjs.org/package/promise-queue-manager)
4[![install size](https://packagephobia.now.sh/badge?p=promise-queue-manager)](https://packagephobia.now.sh/result?p=promise-queue-manager)
5[![language](https://img.shields.io/github/languages/top/gustavolaux/promise-queue-manager?style=flat)](https://www.npmjs.org/package/promise-queue-manager)
6
7A queue manager for concurrent promise execution
8
9## Installation
10
11```
12$ npm i -S promise-queue-manager
13```
14
15## Why use this module?
16
17Sometimes you have to do some large processing using a promise list and you don't want to `Promise.all` then because it will load all the promises into memory and will stop when some error occur. This package can help you with that! You can specify concurrence and set if it can continue processing even if some error occur. It has zero external dependencies and uses `EventEmitter` to control event flow.
18
19## Usage
20
21### Setup
22
23Considering you have a list of promises, you can do this:
24```
25const promises: Promise<any>[] = [];
26
27const config = {
28 promises: promises,
29};
30```
31
32If you have a list of items and a promise to execute it, you can do this:
33```
34const items: any[] = [];
35const promise: () => Promise<any> = (item) => {
36 return new Promise((resolve, reject) => {
37 // do your stuff
38
39 return resolve(); // or reject
40 });
41};
42
43const config = {
44 items: items,
45 promise: promise,
46};
47```
48
49Then you can setup a queue:
50```
51const concurrente = 10;
52const shouldStopOnError = false;
53
54const queue = new PromiseQueue<any>(config, concurrente, shouldStopOnError);
55```
56
57### Listening
58
59Now you can setup your listeners. The `PromiseQueue` class have a static enum that helps you setting up your listeners. They are: `ITEM_ERROR`, `ITEM_PROCESSING`, `ITEM_PROCESSED` and `QUEUE_PROCESSED` and stays on `PromiseQueue.EVENTS`.
60```
61queue.on(PromiseQueue.EVENTS.ITEM_ERROR, (response: PromiseQueueItemResponse<any>) => {
62 console.error(response);
63
64 // you can manually stop the queue calling `.cancel` method
65 queue.cancel();
66});
67
68queue.on(PromiseQueue.EVENTS.ITEM_PROCESSING, (response: PromiseQueueItemResponse<any>) => {
69 console.log(response);
70});
71
72queue.on(PromiseQueue.EVENTS.ITEM_PROCESSED, (response: PromiseQueueItemResponse<any>) => {
73 console.log(response);
74
75 // you can set some rule to cancel the queue anytime you want
76 const canContinue = someMethod();
77 if (!canContinue) queue.cancel();
78});
79
80queue.on(PromiseQueue.EVENTS.QUEUE_PROCESSED, () => {
81 console.log(`Done!`);
82});
83```
84
85Now you can start the queue:
86```
87queue.start();
88```
89
90### Notes
91
92If you set `shouldStopOnError` to `true` you won't receive `PromiseQueue.EVENTS.QUEUE_PROCESSED` event.
93
94## License
95
96promise-queue-manager is freely distributable under the terms of the [MIT license](https://github.com/gustavolaux/promise-queue-manager/blob/master/LICENSE).