@philiprehberger/promise-pool 0.1.6
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 +88 -0
- package/dist/index.cjs +86 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +83 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
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,88 @@
|
|
|
1
|
+
# @philiprehberger/promise-pool
|
|
2
|
+
|
|
3
|
+
[](https://github.com/philiprehberger/ts-promise-pool/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@philiprehberger/promise-pool)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Concurrent promise execution with configurable pool size
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @philiprehberger/promise-pool
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Batch Execution
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { promisePool } from '@philiprehberger/promise-pool';
|
|
21
|
+
|
|
22
|
+
const tasks = urls.map(url => () => fetch(url).then(r => r.json()));
|
|
23
|
+
|
|
24
|
+
const { results, errors } = await promisePool(tasks, {
|
|
25
|
+
concurrency: 5,
|
|
26
|
+
onProgress: ({ completed, total, percent }) => {
|
|
27
|
+
console.log(`${percent}% done (${completed}/${total})`);
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Stop on Error
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const { results, errors } = await promisePool(tasks, {
|
|
36
|
+
concurrency: 3,
|
|
37
|
+
stopOnError: true, // Stop scheduling new tasks after first failure
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Reusable Pool
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { createPool } from '@philiprehberger/promise-pool';
|
|
45
|
+
|
|
46
|
+
const pool = createPool({ concurrency: 3 });
|
|
47
|
+
|
|
48
|
+
// Tasks are queued and run with at most 3 concurrent
|
|
49
|
+
const result1 = pool.run(() => fetch('/api/1'));
|
|
50
|
+
const result2 = pool.run(() => fetch('/api/2'));
|
|
51
|
+
const result3 = pool.run(() => fetch('/api/3'));
|
|
52
|
+
const result4 = pool.run(() => fetch('/api/4')); // waits for a slot
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
| Export | Description |
|
|
58
|
+
|--------|-------------|
|
|
59
|
+
| `promisePool(tasks, options?)` | Execute tasks with concurrency limit, returns `PoolResult` |
|
|
60
|
+
| `createPool(options?)` | Create a reusable pool with `run()` method |
|
|
61
|
+
|
|
62
|
+
### `PoolOptions`
|
|
63
|
+
|
|
64
|
+
| Option | Type | Default | Description |
|
|
65
|
+
|--------|------|---------|-------------|
|
|
66
|
+
| `concurrency` | `number` | `5` | Max concurrent tasks |
|
|
67
|
+
| `stopOnError` | `boolean` | `false` | Stop scheduling after first error |
|
|
68
|
+
| `onProgress` | `(progress) => void` | — | Progress callback |
|
|
69
|
+
|
|
70
|
+
### `PoolResult<T>`
|
|
71
|
+
|
|
72
|
+
| Property | Type | Description |
|
|
73
|
+
|----------|------|-------------|
|
|
74
|
+
| `results` | `(T \| undefined)[]` | Results in original order (`undefined` for failed tasks) |
|
|
75
|
+
| `errors` | `PoolError[]` | Array of `{ index, error }` for failed tasks |
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
## Development
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
npm install
|
|
82
|
+
npm run build
|
|
83
|
+
npm test
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/pool.ts
|
|
4
|
+
async function promisePool(tasks, options = {}) {
|
|
5
|
+
const { concurrency = 5, stopOnError = false, onProgress } = options;
|
|
6
|
+
const total = tasks.length;
|
|
7
|
+
const results = new Array(total).fill(void 0);
|
|
8
|
+
const errors = [];
|
|
9
|
+
let nextIndex = 0;
|
|
10
|
+
let completed = 0;
|
|
11
|
+
let failed = 0;
|
|
12
|
+
let stopped = false;
|
|
13
|
+
function reportProgress() {
|
|
14
|
+
if (onProgress) {
|
|
15
|
+
const progress = {
|
|
16
|
+
completed,
|
|
17
|
+
failed,
|
|
18
|
+
total,
|
|
19
|
+
percent: total > 0 ? Math.round((completed + failed) / total * 100) : 100
|
|
20
|
+
};
|
|
21
|
+
onProgress(progress);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async function runNext() {
|
|
25
|
+
while (nextIndex < total && !stopped) {
|
|
26
|
+
const index = nextIndex++;
|
|
27
|
+
try {
|
|
28
|
+
results[index] = await tasks[index]();
|
|
29
|
+
completed++;
|
|
30
|
+
} catch (error) {
|
|
31
|
+
failed++;
|
|
32
|
+
errors.push({ index, error });
|
|
33
|
+
if (stopOnError) {
|
|
34
|
+
stopped = true;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
reportProgress();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const workers = Array.from(
|
|
42
|
+
{ length: Math.min(concurrency, total) },
|
|
43
|
+
() => runNext()
|
|
44
|
+
);
|
|
45
|
+
await Promise.all(workers);
|
|
46
|
+
return { results, errors };
|
|
47
|
+
}
|
|
48
|
+
function createPool(options = {}) {
|
|
49
|
+
const { concurrency = 5 } = options;
|
|
50
|
+
let active = 0;
|
|
51
|
+
const queue = [];
|
|
52
|
+
function tryRunNext() {
|
|
53
|
+
if (active >= concurrency || queue.length === 0) return;
|
|
54
|
+
const next = queue.shift();
|
|
55
|
+
active++;
|
|
56
|
+
next.task().then(
|
|
57
|
+
(value) => {
|
|
58
|
+
active--;
|
|
59
|
+
next.resolve(value);
|
|
60
|
+
tryRunNext();
|
|
61
|
+
},
|
|
62
|
+
(error) => {
|
|
63
|
+
active--;
|
|
64
|
+
next.reject(error);
|
|
65
|
+
tryRunNext();
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
run(task) {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
queue.push({
|
|
73
|
+
task,
|
|
74
|
+
resolve,
|
|
75
|
+
reject
|
|
76
|
+
});
|
|
77
|
+
tryRunNext();
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
exports.createPool = createPool;
|
|
84
|
+
exports.promisePool = promisePool;
|
|
85
|
+
//# sourceMappingURL=index.cjs.map
|
|
86
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/pool.ts"],"names":[],"mappings":";;;AAEA,eAAsB,WAAA,CACpB,KAAA,EACA,OAAA,GAAuB,EAAC,EACA;AACxB,EAAA,MAAM,EAAE,WAAA,GAAc,CAAA,EAAG,WAAA,GAAc,KAAA,EAAO,YAAW,GAAI,OAAA;AAC7D,EAAA,MAAM,QAAQ,KAAA,CAAM,MAAA;AACpB,EAAA,MAAM,UAA6B,IAAI,KAAA,CAAM,KAAK,CAAA,CAAE,KAAK,MAAS,CAAA;AAClE,EAAA,MAAM,SAAsB,EAAC;AAE7B,EAAA,IAAI,SAAA,GAAY,CAAA;AAChB,EAAA,IAAI,SAAA,GAAY,CAAA;AAChB,EAAA,IAAI,MAAA,GAAS,CAAA;AACb,EAAA,IAAI,OAAA,GAAU,KAAA;AAEd,EAAA,SAAS,cAAA,GAAuB;AAC9B,IAAA,IAAI,UAAA,EAAY;AACd,MAAA,MAAM,QAAA,GAAyB;AAAA,QAC7B,SAAA;AAAA,QACA,MAAA;AAAA,QACA,KAAA;AAAA,QACA,OAAA,EAAS,QAAQ,CAAA,GAAI,IAAA,CAAK,OAAQ,SAAA,GAAY,MAAA,IAAU,KAAA,GAAS,GAAG,CAAA,GAAI;AAAA,OAC1E;AACA,MAAA,UAAA,CAAW,QAAQ,CAAA;AAAA,IACrB;AAAA,EACF;AAEA,EAAA,eAAe,OAAA,GAAyB;AACtC,IAAA,OAAO,SAAA,GAAY,KAAA,IAAS,CAAC,OAAA,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A;AAAA,MACA,CAAC,KAAA,KAAU;AACT,QAAA,MAAA,EAAA;AACA,QAAA,IAAA,CAAK,OAAO,KAAK,CAAA;AACjB,QAAA,UAAA,EAAW;AAAA,MACb;AAAA,KACF;AAAA,EACF;AAEA,EAAA,OAAO;AAAA,IACL,IAAO,IAAA,EAAoC;AACzC,MAAA,OAAO,IAAI,OAAA,CAAW,CAAC,OAAA,EAAS,MAAA,KAAW;AACzC,QAAA,KAAA,CAAM,IAAA,CAAK;AAAA,UACT,IAAA;AAAA,UACA,OAAA;AAAA,UACA;AAAA,SACD,CAAA;AACD,QAAA,UAAA,EAAW;AAAA,MACb,CAAC,CAAA;AAAA,IACH;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["import type { PoolOptions, PoolResult, PoolError, PoolProgress } from './types.js';\n\nexport async function promisePool<T>(\n tasks: (() => Promise<T>)[],\n options: PoolOptions = {},\n): Promise<PoolResult<T>> {\n const { concurrency = 5, stopOnError = false, onProgress } = options;\n const total = tasks.length;\n const results: (T | undefined)[] = new Array(total).fill(undefined);\n const errors: PoolError[] = [];\n\n let nextIndex = 0;\n let completed = 0;\n let failed = 0;\n let stopped = false;\n\n function reportProgress(): void {\n if (onProgress) {\n const progress: PoolProgress = {\n completed,\n failed,\n total,\n percent: total > 0 ? Math.round(((completed + failed) / total) * 100) : 100,\n };\n onProgress(progress);\n }\n }\n\n async function runNext(): Promise<void> {\n while (nextIndex < total && !stopped) {\n const index = nextIndex++;\n\n try {\n results[index] = await tasks[index]();\n completed++;\n } catch (error) {\n failed++;\n errors.push({ index, error });\n\n if (stopOnError) {\n stopped = true;\n return;\n }\n }\n\n reportProgress();\n }\n }\n\n const workers = Array.from(\n { length: Math.min(concurrency, total) },\n () => runNext(),\n );\n\n await Promise.all(workers);\n\n return { results, errors };\n}\n\nexport function createPool(options: PoolOptions = {}): {\n run<T>(task: () => Promise<T>): Promise<T>;\n} {\n const { concurrency = 5 } = options;\n\n let active = 0;\n const queue: Array<{ task: () => Promise<unknown>; resolve: (v: unknown) => void; reject: (e: unknown) => void }> = [];\n\n function tryRunNext(): void {\n if (active >= concurrency || queue.length === 0) return;\n\n const next = queue.shift()!;\n active++;\n\n next.task().then(\n (value) => {\n active--;\n next.resolve(value);\n tryRunNext();\n },\n (error) => {\n active--;\n next.reject(error);\n tryRunNext();\n },\n );\n }\n\n return {\n run<T>(task: () => Promise<T>): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n queue.push({\n task: task as () => Promise<unknown>,\n resolve: resolve as (v: unknown) => void,\n reject,\n });\n tryRunNext();\n });\n },\n };\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface PoolOptions {
|
|
2
|
+
concurrency?: number;
|
|
3
|
+
stopOnError?: boolean;
|
|
4
|
+
onProgress?: (progress: PoolProgress) => void;
|
|
5
|
+
}
|
|
6
|
+
interface PoolProgress {
|
|
7
|
+
completed: number;
|
|
8
|
+
failed: number;
|
|
9
|
+
total: number;
|
|
10
|
+
percent: number;
|
|
11
|
+
}
|
|
12
|
+
interface PoolResult<T> {
|
|
13
|
+
results: (T | undefined)[];
|
|
14
|
+
errors: PoolError[];
|
|
15
|
+
}
|
|
16
|
+
interface PoolError {
|
|
17
|
+
index: number;
|
|
18
|
+
error: unknown;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare function promisePool<T>(tasks: (() => Promise<T>)[], options?: PoolOptions): Promise<PoolResult<T>>;
|
|
22
|
+
declare function createPool(options?: PoolOptions): {
|
|
23
|
+
run<T>(task: () => Promise<T>): Promise<T>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { type PoolError, type PoolOptions, type PoolProgress, type PoolResult, createPool, promisePool };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface PoolOptions {
|
|
2
|
+
concurrency?: number;
|
|
3
|
+
stopOnError?: boolean;
|
|
4
|
+
onProgress?: (progress: PoolProgress) => void;
|
|
5
|
+
}
|
|
6
|
+
interface PoolProgress {
|
|
7
|
+
completed: number;
|
|
8
|
+
failed: number;
|
|
9
|
+
total: number;
|
|
10
|
+
percent: number;
|
|
11
|
+
}
|
|
12
|
+
interface PoolResult<T> {
|
|
13
|
+
results: (T | undefined)[];
|
|
14
|
+
errors: PoolError[];
|
|
15
|
+
}
|
|
16
|
+
interface PoolError {
|
|
17
|
+
index: number;
|
|
18
|
+
error: unknown;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare function promisePool<T>(tasks: (() => Promise<T>)[], options?: PoolOptions): Promise<PoolResult<T>>;
|
|
22
|
+
declare function createPool(options?: PoolOptions): {
|
|
23
|
+
run<T>(task: () => Promise<T>): Promise<T>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { type PoolError, type PoolOptions, type PoolProgress, type PoolResult, createPool, promisePool };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/pool.ts
|
|
2
|
+
async function promisePool(tasks, options = {}) {
|
|
3
|
+
const { concurrency = 5, stopOnError = false, onProgress } = options;
|
|
4
|
+
const total = tasks.length;
|
|
5
|
+
const results = new Array(total).fill(void 0);
|
|
6
|
+
const errors = [];
|
|
7
|
+
let nextIndex = 0;
|
|
8
|
+
let completed = 0;
|
|
9
|
+
let failed = 0;
|
|
10
|
+
let stopped = false;
|
|
11
|
+
function reportProgress() {
|
|
12
|
+
if (onProgress) {
|
|
13
|
+
const progress = {
|
|
14
|
+
completed,
|
|
15
|
+
failed,
|
|
16
|
+
total,
|
|
17
|
+
percent: total > 0 ? Math.round((completed + failed) / total * 100) : 100
|
|
18
|
+
};
|
|
19
|
+
onProgress(progress);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function runNext() {
|
|
23
|
+
while (nextIndex < total && !stopped) {
|
|
24
|
+
const index = nextIndex++;
|
|
25
|
+
try {
|
|
26
|
+
results[index] = await tasks[index]();
|
|
27
|
+
completed++;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
failed++;
|
|
30
|
+
errors.push({ index, error });
|
|
31
|
+
if (stopOnError) {
|
|
32
|
+
stopped = true;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
reportProgress();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const workers = Array.from(
|
|
40
|
+
{ length: Math.min(concurrency, total) },
|
|
41
|
+
() => runNext()
|
|
42
|
+
);
|
|
43
|
+
await Promise.all(workers);
|
|
44
|
+
return { results, errors };
|
|
45
|
+
}
|
|
46
|
+
function createPool(options = {}) {
|
|
47
|
+
const { concurrency = 5 } = options;
|
|
48
|
+
let active = 0;
|
|
49
|
+
const queue = [];
|
|
50
|
+
function tryRunNext() {
|
|
51
|
+
if (active >= concurrency || queue.length === 0) return;
|
|
52
|
+
const next = queue.shift();
|
|
53
|
+
active++;
|
|
54
|
+
next.task().then(
|
|
55
|
+
(value) => {
|
|
56
|
+
active--;
|
|
57
|
+
next.resolve(value);
|
|
58
|
+
tryRunNext();
|
|
59
|
+
},
|
|
60
|
+
(error) => {
|
|
61
|
+
active--;
|
|
62
|
+
next.reject(error);
|
|
63
|
+
tryRunNext();
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
run(task) {
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
queue.push({
|
|
71
|
+
task,
|
|
72
|
+
resolve,
|
|
73
|
+
reject
|
|
74
|
+
});
|
|
75
|
+
tryRunNext();
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { createPool, promisePool };
|
|
82
|
+
//# sourceMappingURL=index.js.map
|
|
83
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/pool.ts"],"names":[],"mappings":";AAEA,eAAsB,WAAA,CACpB,KAAA,EACA,O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C,CAAA;AAAA,IACH;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import type { PoolOptions, PoolResult, PoolError, PoolProgress } from './types.js';\n\nexport async function promisePool<T>(\n tasks: (() => Promise<T>)[],\n options: PoolOptions = {},\n): Promise<PoolResult<T>> {\n const { concurrency = 5, stopOnError = false, onProgress } = options;\n const total = tasks.length;\n const results: (T | undefined)[] = new Array(total).fill(undefined);\n const errors: PoolError[] = [];\n\n let nextIndex = 0;\n let completed = 0;\n let failed = 0;\n let stopped = false;\n\n function reportProgress(): void {\n if (onProgress) {\n const progress: PoolProgress = {\n completed,\n failed,\n total,\n percent: total > 0 ? Math.round(((completed + failed) / total) * 100) : 100,\n };\n onProgress(progress);\n }\n }\n\n async function runNext(): Promise<void> {\n while (nextIndex < total && !stopped) {\n const index = nextIndex++;\n\n try {\n results[index] = await tasks[index]();\n completed++;\n } catch (error) {\n failed++;\n errors.push({ index, error });\n\n if (stopOnError) {\n stopped = true;\n return;\n }\n }\n\n reportProgress();\n }\n }\n\n const workers = Array.from(\n { length: Math.min(concurrency, total) },\n () => runNext(),\n );\n\n await Promise.all(workers);\n\n return { results, errors };\n}\n\nexport function createPool(options: PoolOptions = {}): {\n run<T>(task: () => Promise<T>): Promise<T>;\n} {\n const { concurrency = 5 } = options;\n\n let active = 0;\n const queue: Array<{ task: () => Promise<unknown>; resolve: (v: unknown) => void; reject: (e: unknown) => void }> = [];\n\n function tryRunNext(): void {\n if (active >= concurrency || queue.length === 0) return;\n\n const next = queue.shift()!;\n active++;\n\n next.task().then(\n (value) => {\n active--;\n next.resolve(value);\n tryRunNext();\n },\n (error) => {\n active--;\n next.reject(error);\n tryRunNext();\n },\n );\n }\n\n return {\n run<T>(task: () => Promise<T>): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n queue.push({\n task: task as () => Promise<unknown>,\n resolve: resolve as (v: unknown) => void,\n reject,\n });\n tryRunNext();\n });\n },\n };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@philiprehberger/promise-pool",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "Concurrent promise execution with configurable pool size",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"dev": "tsup --watch",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"prepublishOnly": "npm run build",
|
|
29
|
+
"test": "node --test"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsup": "^8.0.0",
|
|
33
|
+
"typescript": "^5.0.0"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"promise",
|
|
37
|
+
"pool",
|
|
38
|
+
"concurrency",
|
|
39
|
+
"parallel",
|
|
40
|
+
"async",
|
|
41
|
+
"queue"
|
|
42
|
+
],
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/philiprehberger/ts-promise-pool.git"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/philiprehberger/ts-promise-pool#readme",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/philiprehberger/ts-promise-pool/issues"
|
|
51
|
+
},
|
|
52
|
+
"author": "Philip Rehberger",
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=18.0.0"
|
|
55
|
+
},
|
|
56
|
+
"sideEffects": false
|
|
57
|
+
}
|