UNPKG

3.79 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```sh
13npm 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## Upgrading
21
22### From 2.x.x to 3.x.x
23
24- Old constructor parameters `concurrence` and `shouldStopOnError` are now passed in `config` object.
25- The `promises` list parameter now require a function that returns a promise to avoid early promise execution. (Huge thanks to [@dArignac](https://github.com/dArignac) with [this issue](https://github.com/gustavolaux/promise-queue-manager/issues/16))
26
27### From 1.x.x to 2.x.x
28
29- `PromiseQueue.EVENTS.QUEUE_PROCESSED` is now fired even if `shouldStopOnError` is set to `true`.
30
31## Usage
32
33### Demo
34
35You can access a repl demo [here](https://repl.it/@gustavolaux/promise-queue-manager-demo)
36
37### Setup
38
39You can use this lib in two ways: with a list of functions that return a promise or with a promise and a list of items to process. In both cases:
40
41```ts
42const saveOnDatabase = async (data) => {
43 const result = await repository.save(data);
44
45 return result;
46};
47
48const config = {
49 concurrence: 10,
50 shouldStopOnError: true,
51};
52```
53
54Using a list of promises:
55```ts
56const items = [
57 { name: 'foo' },
58 { name: 'bar' },
59];
60
61// you need to wrap your promise inside a function
62// to avoid early calls
63config.promises = items.map(item => () => saveOnDatabase(item));
64```
65
66Using a list of items:
67```ts
68const items = [
69 { name: 'foo' },
70 { name: 'bar' },
71];
72
73config.promise = saveOnDatabase;
74config.items = items;
75```
76
77Now you can initialize the queue:
78```ts
79const queue = new PromiseQueue<YourInterface>(config);
80```
81
82### Listening
83
84Now 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`.
85```ts
86queue.on(PromiseQueue.EVENTS.ITEM_ERROR, (response: PromiseQueueItemResponse<any>) => {
87 console.error(response);
88
89 // you can manually stop the queue calling `.cancel` method
90 queue.cancel();
91});
92
93// useful only if `items` is used
94queue.on(PromiseQueue.EVENTS.ITEM_PROCESSING, (response: PromiseQueueItemResponse<any>) => {
95 console.log(response);
96});
97
98queue.on(PromiseQueue.EVENTS.ITEM_PROCESSED, (response: PromiseQueueItemResponse<any>) => {
99 console.log(response);
100
101 // you can set some rule to cancel the queue anytime you want
102 const canContinue = someMethod();
103 if (!canContinue) queue.cancel();
104});
105
106queue.on(PromiseQueue.EVENTS.QUEUE_PROCESSED, () => {
107 console.log(`Done!`);
108});
109```
110
111Now you can start the queue:
112```ts
113queue.start();
114```
115
116## License
117
118promise-queue-manager is freely distributable under the terms of the [MIT license](https://github.com/gustavolaux/promise-queue-manager/blob/master/LICENSE).