UNPKG

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