simple-p-limit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +217 -0
- package/lib/index.js +42 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
<!-- Please do not edit this file. Edit the `blah` field in the `package.json` instead. If in doubt, open an issue. -->
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# simple-p-limit
|
|
21
|
+
|
|
22
|
+
[![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [](https://github.com/IonicaBizau/ama) [](https://www.npmjs.com/package/simple-p-limit) [](https://www.npmjs.com/package/simple-p-limit) [](https://www.codementor.io/@johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github)
|
|
23
|
+
|
|
24
|
+
<a href="https://www.buymeacoffee.com/H96WwChMy" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png" alt="Buy Me A Coffee"></a>
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
> A simple implementation of concurent promises with a limit.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## :cloud: Installation
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
# Using npm
|
|
54
|
+
npm install --save simple-p-limit
|
|
55
|
+
|
|
56
|
+
# Using yarn
|
|
57
|
+
yarn add simple-p-limit
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## :clipboard: Example
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
const simplePLimit = require("simple-p-limit");
|
|
78
|
+
|
|
79
|
+
(async () => {
|
|
80
|
+
const limit = simplePLimit(2)
|
|
81
|
+
|
|
82
|
+
// Time ------------------------>
|
|
83
|
+
// Task 1 |--- 1000ms ---|
|
|
84
|
+
// Task 2 |-500ms|
|
|
85
|
+
// Task 3 |-300ms|
|
|
86
|
+
// Task 4 |--700ms-|
|
|
87
|
+
const sleep = (ms, value) => new Promise(resolve => setTimeout(() => resolve(value), ms));
|
|
88
|
+
const tasks = [
|
|
89
|
+
limit(() => { return sleep(1000, 'Task 1'); }),
|
|
90
|
+
limit(() => { return sleep(500, 'Task 2'); }),
|
|
91
|
+
limit(() => { return sleep(300, 'Task 3'); }),
|
|
92
|
+
limit(() => { return sleep(700, 'Task 4'); }),
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
console.log(await Promise.all(tasks)); // Output: ['Task 1', 'Task 2', 'Task 3', 'Task 4']
|
|
96
|
+
})();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
## :memo: Documentation
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
### `simplePLimit(concurrency)`
|
|
113
|
+
A simple implementation of concurent promises with a limit.
|
|
114
|
+
|
|
115
|
+
#### Params
|
|
116
|
+
|
|
117
|
+
- **Number** `concurrency`: How many functions returning a promise should run in parallel.
|
|
118
|
+
|
|
119
|
+
#### Return
|
|
120
|
+
- **Function** The `limit` function.
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
## :question: Get Help
|
|
131
|
+
|
|
132
|
+
There are few ways to get help:
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question.
|
|
137
|
+
2. For bug reports and feature requests, open issues. :bug:
|
|
138
|
+
3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
## :yum: How to contribute
|
|
154
|
+
Have an idea? Found a bug? See [how to contribute][contributing].
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
## :sparkling_heart: Support my projects
|
|
158
|
+
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously,
|
|
159
|
+
this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute (even resell it).
|
|
160
|
+
|
|
161
|
+
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
- Starring and sharing the projects you like :rocket:
|
|
165
|
+
- [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you after years if you buy me one. :grin: :book:
|
|
166
|
+
- [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea:
|
|
167
|
+
- [![Support me on Patreon][badge_patreon]][patreon]—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
|
|
168
|
+
- **Bitcoin**—You can send me bitcoins at this address (or scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6`
|
|
169
|
+
|
|
170
|
+

|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
Thanks! :heart:
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
## :scroll: License
|
|
199
|
+
|
|
200
|
+
[MIT][license] © [Ionică Bizău][website]
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
[license]: /LICENSE
|
|
208
|
+
[website]: https://ionicabizau.net
|
|
209
|
+
[contributing]: /CONTRIBUTING.md
|
|
210
|
+
[docs]: /DOCUMENTATION.md
|
|
211
|
+
[badge_patreon]: https://ionicabizau.github.io/badges/patreon.svg
|
|
212
|
+
[badge_amazon]: https://ionicabizau.github.io/badges/amazon.svg
|
|
213
|
+
[badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg
|
|
214
|
+
[badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg
|
|
215
|
+
[patreon]: https://www.patreon.com/ionicabizau
|
|
216
|
+
[amazon]: http://amzn.eu/hRo9sIZ
|
|
217
|
+
[paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* simplePLimit
|
|
5
|
+
* A simple implementation of concurent promises with a limit.
|
|
6
|
+
*
|
|
7
|
+
* @name simplePLimit
|
|
8
|
+
* @function
|
|
9
|
+
* @param {Number} concurrency How many functions returning a promise should run in parallel.
|
|
10
|
+
* @return {Function} The `limit` function.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
module.exports = function simplePLimit(concurrency) {
|
|
14
|
+
var queue = [];
|
|
15
|
+
var activeCount = 0;
|
|
16
|
+
|
|
17
|
+
async function next() {
|
|
18
|
+
if (queue.length === 0 || activeCount >= concurrency) return;
|
|
19
|
+
activeCount++;
|
|
20
|
+
|
|
21
|
+
var _queue$shift = queue.shift(),
|
|
22
|
+
fn = _queue$shift.fn,
|
|
23
|
+
resolve = _queue$shift.resolve,
|
|
24
|
+
reject = _queue$shift.reject;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
resolve((await fn()));
|
|
28
|
+
} catch (error) {
|
|
29
|
+
reject(error);
|
|
30
|
+
} finally {
|
|
31
|
+
activeCount--;
|
|
32
|
+
next();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return function (fn) {
|
|
37
|
+
return new Promise(function (resolve, reject) {
|
|
38
|
+
queue.push({ fn: fn, resolve: resolve, reject: reject });
|
|
39
|
+
next();
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "simple-p-limit",
|
|
3
|
+
"description": "A simple implementation of concurent promises with a limit.",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"simple",
|
|
6
|
+
"limit",
|
|
7
|
+
"implementation",
|
|
8
|
+
"concurent",
|
|
9
|
+
"promises"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"version": "1.0.0",
|
|
13
|
+
"main": "lib/index.js",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
16
|
+
},
|
|
17
|
+
"author": "Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)",
|
|
18
|
+
"homepage": "https://github.com/IonicaBizau/simple-p-limit#readme",
|
|
19
|
+
"files": [
|
|
20
|
+
"bin/",
|
|
21
|
+
"app/",
|
|
22
|
+
"lib/",
|
|
23
|
+
"dist/",
|
|
24
|
+
"src/",
|
|
25
|
+
"scripts/",
|
|
26
|
+
"resources/",
|
|
27
|
+
"menu/",
|
|
28
|
+
"cli.js",
|
|
29
|
+
"index.js",
|
|
30
|
+
"index.d.ts",
|
|
31
|
+
"package-lock.json"
|
|
32
|
+
],
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+ssh://git@github.com/IonicaBizau/simple-p-limit.git"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/IonicaBizau/simple-p-limit/issues"
|
|
39
|
+
}
|
|
40
|
+
}
|