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 |
|
7 | A 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 |
|
17 | Sometimes 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 |
|
23 | Considering you have a list of promises, you can do this:
|
24 | ```
|
25 | const promises: Promise<any>[] = [];
|
26 |
|
27 | const config = {
|
28 | promises: promises,
|
29 | };
|
30 | ```
|
31 |
|
32 | If you have a list of items and a promise to execute it, you can do this:
|
33 | ```
|
34 | const items: any[] = [];
|
35 | const promise: () => Promise<any> = (item) => {
|
36 | return new Promise((resolve, reject) => {
|
37 | // do your stuff
|
38 |
|
39 | return resolve(); // or reject
|
40 | });
|
41 | };
|
42 |
|
43 | const config = {
|
44 | items: items,
|
45 | promise: promise,
|
46 | };
|
47 | ```
|
48 |
|
49 | Then you can setup a queue:
|
50 | ```
|
51 | const concurrente = 10;
|
52 | const shouldStopOnError = false;
|
53 |
|
54 | const queue = new PromiseQueue<any>(config, concurrente, shouldStopOnError);
|
55 | ```
|
56 |
|
57 | ### Listening
|
58 |
|
59 | Now 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 | ```
|
61 | queue.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 |
|
68 | queue.on(PromiseQueue.EVENTS.ITEM_PROCESSING, (response: PromiseQueueItemResponse<any>) => {
|
69 | console.log(response);
|
70 | });
|
71 |
|
72 | queue.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 |
|
80 | queue.on(PromiseQueue.EVENTS.QUEUE_PROCESSED, () => {
|
81 | console.log(`Done!`);
|
82 | });
|
83 | ```
|
84 |
|
85 | Now you can start the queue:
|
86 | ```
|
87 | queue.start();
|
88 | ```
|
89 |
|
90 | ### Notes
|
91 |
|
92 | If you set `shouldStopOnError` to `true` you won't receive `PromiseQueue.EVENTS.QUEUE_PROCESSED` event.
|
93 |
|
94 | ## License
|
95 |
|
96 | promise-queue-manager is freely distributable under the terms of the [MIT license](https://github.com/gustavolaux/promise-queue-manager/blob/master/LICENSE).
|