es-toolkit 1.50.0-dev.2048 → 1.50.0-dev.2050
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/dist/array/filterAsync.js +1 -1
- package/dist/array/filterAsync.mjs +1 -1
- package/dist/array/flatMapAsync.js +1 -1
- package/dist/array/flatMapAsync.mjs +1 -1
- package/dist/array/forEachAsync.js +1 -1
- package/dist/array/forEachAsync.mjs +1 -1
- package/dist/array/index.js +1 -1
- package/dist/array/index.mjs +1 -1
- package/dist/array/limitAsync.d.mts +4 -28
- package/dist/array/limitAsync.d.ts +4 -28
- package/dist/array/limitAsync.js +2 -39
- package/dist/array/limitAsync.mjs +3 -39
- package/dist/array/mapAsync.js +1 -1
- package/dist/array/mapAsync.mjs +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/promise/index.d.mts +2 -1
- package/dist/promise/index.d.ts +2 -1
- package/dist/promise/index.js +2 -0
- package/dist/promise/index.mjs +2 -1
- package/dist/promise/limitAsync.d.mts +35 -0
- package/dist/promise/limitAsync.d.ts +35 -0
- package/dist/promise/limitAsync.js +46 -0
- package/dist/promise/limitAsync.mjs +46 -0
- package/package.json +1 -1
package/dist/array/index.js
CHANGED
|
@@ -14,7 +14,6 @@ const require_dropRight = require("./dropRight.js");
|
|
|
14
14
|
const require_dropRightWhile = require("./dropRightWhile.js");
|
|
15
15
|
const require_dropWhile = require("./dropWhile.js");
|
|
16
16
|
const require_fill = require("./fill.js");
|
|
17
|
-
const require_limitAsync = require("./limitAsync.js");
|
|
18
17
|
const require_filterAsync = require("./filterAsync.js");
|
|
19
18
|
const require_flatten = require("./flatten.js");
|
|
20
19
|
const require_flatMap = require("./flatMap.js");
|
|
@@ -33,6 +32,7 @@ const require_isSubset = require("./isSubset.js");
|
|
|
33
32
|
const require_isSubsetWith = require("./isSubsetWith.js");
|
|
34
33
|
const require_keyBy = require("./keyBy.js");
|
|
35
34
|
const require_last = require("./last.js");
|
|
35
|
+
const require_limitAsync = require("./limitAsync.js");
|
|
36
36
|
const require_mapAsync = require("./mapAsync.js");
|
|
37
37
|
const require_maxBy = require("./maxBy.js");
|
|
38
38
|
const require_minBy = require("./minBy.js");
|
package/dist/array/index.mjs
CHANGED
|
@@ -13,7 +13,6 @@ import { dropRight } from "./dropRight.mjs";
|
|
|
13
13
|
import { dropRightWhile } from "./dropRightWhile.mjs";
|
|
14
14
|
import { dropWhile } from "./dropWhile.mjs";
|
|
15
15
|
import { fill } from "./fill.mjs";
|
|
16
|
-
import { limitAsync } from "./limitAsync.mjs";
|
|
17
16
|
import { filterAsync } from "./filterAsync.mjs";
|
|
18
17
|
import { flatten } from "./flatten.mjs";
|
|
19
18
|
import { flatMap } from "./flatMap.mjs";
|
|
@@ -32,6 +31,7 @@ import { isSubset } from "./isSubset.mjs";
|
|
|
32
31
|
import { isSubsetWith } from "./isSubsetWith.mjs";
|
|
33
32
|
import { keyBy } from "./keyBy.mjs";
|
|
34
33
|
import { last } from "./last.mjs";
|
|
34
|
+
import { limitAsync } from "./limitAsync.mjs";
|
|
35
35
|
import { mapAsync } from "./mapAsync.mjs";
|
|
36
36
|
import { maxBy } from "./maxBy.mjs";
|
|
37
37
|
import { minBy } from "./minBy.mjs";
|
|
@@ -1,35 +1,11 @@
|
|
|
1
|
+
import { limitAsync as limitAsync$1 } from "../promise/limitAsync.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/array/limitAsync.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Wraps an async function to limit the number of concurrent executions.
|
|
4
6
|
*
|
|
5
|
-
* This
|
|
6
|
-
* `concurrency` number of executions can run simultaneously. Additional calls will
|
|
7
|
-
* wait until a slot becomes available.
|
|
8
|
-
*
|
|
9
|
-
* @template F - The type of the async function to wrap.
|
|
10
|
-
* @param callback The async function to wrap with concurrency control.
|
|
11
|
-
* @param concurrency Maximum number of concurrent executions allowed.
|
|
12
|
-
* @returns A wrapped version of the callback with concurrency limiting.
|
|
13
|
-
* @example
|
|
14
|
-
* const limitedFetch = limitAsync(async (url) => {
|
|
15
|
-
* return await fetch(url);
|
|
16
|
-
* }, 3);
|
|
17
|
-
*
|
|
18
|
-
* // Only 3 fetches will run concurrently
|
|
19
|
-
* const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
|
|
20
|
-
* await Promise.all(urls.map(url => limitedFetch(url)));
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* const processItem = async (item) => {
|
|
24
|
-
* // Expensive async operation
|
|
25
|
-
* return await heavyComputation(item);
|
|
26
|
-
* };
|
|
27
|
-
*
|
|
28
|
-
* const limitedProcess = limitAsync(processItem, 2);
|
|
29
|
-
* const items = [1, 2, 3, 4, 5];
|
|
30
|
-
* // At most 2 items will be processed concurrently
|
|
31
|
-
* await Promise.all(items.map(item => limitedProcess(item)));
|
|
7
|
+
* @deprecated Use `limitAsync` from `es-toolkit/promise` instead. This export will be removed from `es-toolkit/array` in a future major version.
|
|
32
8
|
*/
|
|
33
|
-
declare
|
|
9
|
+
declare const limitAsync: typeof limitAsync$1;
|
|
34
10
|
//#endregion
|
|
35
11
|
export { limitAsync };
|
|
@@ -1,35 +1,11 @@
|
|
|
1
|
+
import { limitAsync as limitAsync$1 } from "../promise/limitAsync.js";
|
|
2
|
+
|
|
1
3
|
//#region src/array/limitAsync.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Wraps an async function to limit the number of concurrent executions.
|
|
4
6
|
*
|
|
5
|
-
* This
|
|
6
|
-
* `concurrency` number of executions can run simultaneously. Additional calls will
|
|
7
|
-
* wait until a slot becomes available.
|
|
8
|
-
*
|
|
9
|
-
* @template F - The type of the async function to wrap.
|
|
10
|
-
* @param callback The async function to wrap with concurrency control.
|
|
11
|
-
* @param concurrency Maximum number of concurrent executions allowed.
|
|
12
|
-
* @returns A wrapped version of the callback with concurrency limiting.
|
|
13
|
-
* @example
|
|
14
|
-
* const limitedFetch = limitAsync(async (url) => {
|
|
15
|
-
* return await fetch(url);
|
|
16
|
-
* }, 3);
|
|
17
|
-
*
|
|
18
|
-
* // Only 3 fetches will run concurrently
|
|
19
|
-
* const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
|
|
20
|
-
* await Promise.all(urls.map(url => limitedFetch(url)));
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* const processItem = async (item) => {
|
|
24
|
-
* // Expensive async operation
|
|
25
|
-
* return await heavyComputation(item);
|
|
26
|
-
* };
|
|
27
|
-
*
|
|
28
|
-
* const limitedProcess = limitAsync(processItem, 2);
|
|
29
|
-
* const items = [1, 2, 3, 4, 5];
|
|
30
|
-
* // At most 2 items will be processed concurrently
|
|
31
|
-
* await Promise.all(items.map(item => limitedProcess(item)));
|
|
7
|
+
* @deprecated Use `limitAsync` from `es-toolkit/promise` instead. This export will be removed from `es-toolkit/array` in a future major version.
|
|
32
8
|
*/
|
|
33
|
-
declare
|
|
9
|
+
declare const limitAsync: typeof limitAsync$1;
|
|
34
10
|
//#endregion
|
|
35
11
|
export { limitAsync };
|
package/dist/array/limitAsync.js
CHANGED
|
@@ -1,46 +1,9 @@
|
|
|
1
|
-
const require_semaphore = require("../promise/semaphore.js");
|
|
2
1
|
//#region src/array/limitAsync.ts
|
|
3
2
|
/**
|
|
4
3
|
* Wraps an async function to limit the number of concurrent executions.
|
|
5
4
|
*
|
|
6
|
-
* This
|
|
7
|
-
* `concurrency` number of executions can run simultaneously. Additional calls will
|
|
8
|
-
* wait until a slot becomes available.
|
|
9
|
-
*
|
|
10
|
-
* @template F - The type of the async function to wrap.
|
|
11
|
-
* @param callback The async function to wrap with concurrency control.
|
|
12
|
-
* @param concurrency Maximum number of concurrent executions allowed.
|
|
13
|
-
* @returns A wrapped version of the callback with concurrency limiting.
|
|
14
|
-
* @example
|
|
15
|
-
* const limitedFetch = limitAsync(async (url) => {
|
|
16
|
-
* return await fetch(url);
|
|
17
|
-
* }, 3);
|
|
18
|
-
*
|
|
19
|
-
* // Only 3 fetches will run concurrently
|
|
20
|
-
* const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
|
|
21
|
-
* await Promise.all(urls.map(url => limitedFetch(url)));
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* const processItem = async (item) => {
|
|
25
|
-
* // Expensive async operation
|
|
26
|
-
* return await heavyComputation(item);
|
|
27
|
-
* };
|
|
28
|
-
*
|
|
29
|
-
* const limitedProcess = limitAsync(processItem, 2);
|
|
30
|
-
* const items = [1, 2, 3, 4, 5];
|
|
31
|
-
* // At most 2 items will be processed concurrently
|
|
32
|
-
* await Promise.all(items.map(item => limitedProcess(item)));
|
|
5
|
+
* @deprecated Use `limitAsync` from `es-toolkit/promise` instead. This export will be removed from `es-toolkit/array` in a future major version.
|
|
33
6
|
*/
|
|
34
|
-
|
|
35
|
-
const semaphore = new require_semaphore.Semaphore(concurrency);
|
|
36
|
-
return async function(...args) {
|
|
37
|
-
try {
|
|
38
|
-
await semaphore.acquire();
|
|
39
|
-
return await callback.apply(this, args);
|
|
40
|
-
} finally {
|
|
41
|
-
semaphore.release();
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
7
|
+
const limitAsync = require("../promise/limitAsync.js").limitAsync;
|
|
45
8
|
//#endregion
|
|
46
9
|
exports.limitAsync = limitAsync;
|
|
@@ -1,46 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { limitAsync as limitAsync$1 } from "../promise/limitAsync.mjs";
|
|
2
2
|
//#region src/array/limitAsync.ts
|
|
3
3
|
/**
|
|
4
4
|
* Wraps an async function to limit the number of concurrent executions.
|
|
5
5
|
*
|
|
6
|
-
* This
|
|
7
|
-
* `concurrency` number of executions can run simultaneously. Additional calls will
|
|
8
|
-
* wait until a slot becomes available.
|
|
9
|
-
*
|
|
10
|
-
* @template F - The type of the async function to wrap.
|
|
11
|
-
* @param callback The async function to wrap with concurrency control.
|
|
12
|
-
* @param concurrency Maximum number of concurrent executions allowed.
|
|
13
|
-
* @returns A wrapped version of the callback with concurrency limiting.
|
|
14
|
-
* @example
|
|
15
|
-
* const limitedFetch = limitAsync(async (url) => {
|
|
16
|
-
* return await fetch(url);
|
|
17
|
-
* }, 3);
|
|
18
|
-
*
|
|
19
|
-
* // Only 3 fetches will run concurrently
|
|
20
|
-
* const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
|
|
21
|
-
* await Promise.all(urls.map(url => limitedFetch(url)));
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* const processItem = async (item) => {
|
|
25
|
-
* // Expensive async operation
|
|
26
|
-
* return await heavyComputation(item);
|
|
27
|
-
* };
|
|
28
|
-
*
|
|
29
|
-
* const limitedProcess = limitAsync(processItem, 2);
|
|
30
|
-
* const items = [1, 2, 3, 4, 5];
|
|
31
|
-
* // At most 2 items will be processed concurrently
|
|
32
|
-
* await Promise.all(items.map(item => limitedProcess(item)));
|
|
6
|
+
* @deprecated Use `limitAsync` from `es-toolkit/promise` instead. This export will be removed from `es-toolkit/array` in a future major version.
|
|
33
7
|
*/
|
|
34
|
-
|
|
35
|
-
const semaphore = new Semaphore(concurrency);
|
|
36
|
-
return async function(...args) {
|
|
37
|
-
try {
|
|
38
|
-
await semaphore.acquire();
|
|
39
|
-
return await callback.apply(this, args);
|
|
40
|
-
} finally {
|
|
41
|
-
semaphore.release();
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
8
|
+
const limitAsync = limitAsync$1;
|
|
45
9
|
//#endregion
|
|
46
10
|
export { limitAsync };
|
package/dist/array/mapAsync.js
CHANGED
package/dist/array/mapAsync.mjs
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -31,7 +31,7 @@ import { isSubset } from "./array/isSubset.mjs";
|
|
|
31
31
|
import { isSubsetWith } from "./array/isSubsetWith.mjs";
|
|
32
32
|
import { keyBy } from "./array/keyBy.mjs";
|
|
33
33
|
import { last } from "./array/last.mjs";
|
|
34
|
-
import { limitAsync } from "./
|
|
34
|
+
import { limitAsync } from "./promise/limitAsync.mjs";
|
|
35
35
|
import { mapAsync } from "./array/mapAsync.mjs";
|
|
36
36
|
import { maxBy } from "./array/maxBy.mjs";
|
|
37
37
|
import { minBy } from "./array/minBy.mjs";
|
package/dist/index.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ import { isSubset } from "./array/isSubset.js";
|
|
|
31
31
|
import { isSubsetWith } from "./array/isSubsetWith.js";
|
|
32
32
|
import { keyBy } from "./array/keyBy.js";
|
|
33
33
|
import { last } from "./array/last.js";
|
|
34
|
-
import { limitAsync } from "./
|
|
34
|
+
import { limitAsync } from "./promise/limitAsync.js";
|
|
35
35
|
import { mapAsync } from "./array/mapAsync.js";
|
|
36
36
|
import { maxBy } from "./array/maxBy.js";
|
|
37
37
|
import { minBy } from "./array/minBy.js";
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@ const require_dropRightWhile = require("./array/dropRightWhile.js");
|
|
|
15
15
|
const require_dropWhile = require("./array/dropWhile.js");
|
|
16
16
|
const require_fill = require("./array/fill.js");
|
|
17
17
|
const require_semaphore = require("./promise/semaphore.js");
|
|
18
|
-
const require_limitAsync = require("./
|
|
18
|
+
const require_limitAsync = require("./promise/limitAsync.js");
|
|
19
19
|
const require_filterAsync = require("./array/filterAsync.js");
|
|
20
20
|
const require_flatten = require("./array/flatten.js");
|
|
21
21
|
const require_flatMap = require("./array/flatMap.js");
|
package/dist/index.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import { dropRightWhile } from "./array/dropRightWhile.mjs";
|
|
|
14
14
|
import { dropWhile } from "./array/dropWhile.mjs";
|
|
15
15
|
import { fill } from "./array/fill.mjs";
|
|
16
16
|
import { Semaphore } from "./promise/semaphore.mjs";
|
|
17
|
-
import { limitAsync } from "./
|
|
17
|
+
import { limitAsync } from "./promise/limitAsync.mjs";
|
|
18
18
|
import { filterAsync } from "./array/filterAsync.mjs";
|
|
19
19
|
import { flatten } from "./array/flatten.mjs";
|
|
20
20
|
import { flatMap } from "./array/flatMap.mjs";
|
package/dist/promise/index.d.mts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { limitAsync } from "./limitAsync.mjs";
|
|
1
2
|
import { allKeyed } from "./allKeyed.mjs";
|
|
2
3
|
import { delay } from "./delay.mjs";
|
|
3
4
|
import { Mutex } from "./mutex.mjs";
|
|
4
5
|
import { Semaphore } from "./semaphore.mjs";
|
|
5
6
|
import { timeout } from "./timeout.mjs";
|
|
6
7
|
import { withTimeout } from "./withTimeout.mjs";
|
|
7
|
-
export { Mutex, Semaphore, allKeyed, delay, timeout, withTimeout };
|
|
8
|
+
export { Mutex, Semaphore, allKeyed, delay, limitAsync, timeout, withTimeout };
|
package/dist/promise/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { limitAsync } from "./limitAsync.js";
|
|
1
2
|
import { allKeyed } from "./allKeyed.js";
|
|
2
3
|
import { delay } from "./delay.js";
|
|
3
4
|
import { Mutex } from "./mutex.js";
|
|
4
5
|
import { Semaphore } from "./semaphore.js";
|
|
5
6
|
import { timeout } from "./timeout.js";
|
|
6
7
|
import { withTimeout } from "./withTimeout.js";
|
|
7
|
-
export { Mutex, Semaphore, allKeyed, delay, timeout, withTimeout };
|
|
8
|
+
export { Mutex, Semaphore, allKeyed, delay, limitAsync, timeout, withTimeout };
|
package/dist/promise/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_semaphore = require("./semaphore.js");
|
|
3
|
+
const require_limitAsync = require("./limitAsync.js");
|
|
3
4
|
const require_delay = require("./delay.js");
|
|
4
5
|
const require_allKeyed = require("./allKeyed.js");
|
|
5
6
|
const require_mutex = require("./mutex.js");
|
|
@@ -9,5 +10,6 @@ exports.Mutex = require_mutex.Mutex;
|
|
|
9
10
|
exports.Semaphore = require_semaphore.Semaphore;
|
|
10
11
|
exports.allKeyed = require_allKeyed.allKeyed;
|
|
11
12
|
exports.delay = require_delay.delay;
|
|
13
|
+
exports.limitAsync = require_limitAsync.limitAsync;
|
|
12
14
|
exports.timeout = require_timeout.timeout;
|
|
13
15
|
exports.withTimeout = require_withTimeout.withTimeout;
|
package/dist/promise/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Semaphore } from "./semaphore.mjs";
|
|
2
|
+
import { limitAsync } from "./limitAsync.mjs";
|
|
2
3
|
import { delay } from "./delay.mjs";
|
|
3
4
|
import { allKeyed } from "./allKeyed.mjs";
|
|
4
5
|
import { Mutex } from "./mutex.mjs";
|
|
5
6
|
import { timeout } from "./timeout.mjs";
|
|
6
7
|
import { withTimeout } from "./withTimeout.mjs";
|
|
7
|
-
export { Mutex, Semaphore, allKeyed, delay, timeout, withTimeout };
|
|
8
|
+
export { Mutex, Semaphore, allKeyed, delay, limitAsync, timeout, withTimeout };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region src/promise/limitAsync.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Wraps an async function to limit the number of concurrent executions.
|
|
4
|
+
*
|
|
5
|
+
* This function creates a wrapper around an async callback that ensures at most
|
|
6
|
+
* `concurrency` number of executions can run simultaneously. Additional calls will
|
|
7
|
+
* wait until a slot becomes available.
|
|
8
|
+
*
|
|
9
|
+
* @template F - The type of the async function to wrap.
|
|
10
|
+
* @param callback The async function to wrap with concurrency control.
|
|
11
|
+
* @param concurrency Maximum number of concurrent executions allowed.
|
|
12
|
+
* @returns A wrapped version of the callback with concurrency limiting.
|
|
13
|
+
* @example
|
|
14
|
+
* const limitedFetch = limitAsync(async (url) => {
|
|
15
|
+
* return await fetch(url);
|
|
16
|
+
* }, 3);
|
|
17
|
+
*
|
|
18
|
+
* // Only 3 fetches will run concurrently
|
|
19
|
+
* const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
|
|
20
|
+
* await Promise.all(urls.map(url => limitedFetch(url)));
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const processItem = async (item) => {
|
|
24
|
+
* // Expensive async operation
|
|
25
|
+
* return await heavyComputation(item);
|
|
26
|
+
* };
|
|
27
|
+
*
|
|
28
|
+
* const limitedProcess = limitAsync(processItem, 2);
|
|
29
|
+
* const items = [1, 2, 3, 4, 5];
|
|
30
|
+
* // At most 2 items will be processed concurrently
|
|
31
|
+
* await Promise.all(items.map(item => limitedProcess(item)));
|
|
32
|
+
*/
|
|
33
|
+
declare function limitAsync<F extends (...args: any[]) => Promise<any>>(callback: F, concurrency: number): F;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { limitAsync };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region src/promise/limitAsync.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Wraps an async function to limit the number of concurrent executions.
|
|
4
|
+
*
|
|
5
|
+
* This function creates a wrapper around an async callback that ensures at most
|
|
6
|
+
* `concurrency` number of executions can run simultaneously. Additional calls will
|
|
7
|
+
* wait until a slot becomes available.
|
|
8
|
+
*
|
|
9
|
+
* @template F - The type of the async function to wrap.
|
|
10
|
+
* @param callback The async function to wrap with concurrency control.
|
|
11
|
+
* @param concurrency Maximum number of concurrent executions allowed.
|
|
12
|
+
* @returns A wrapped version of the callback with concurrency limiting.
|
|
13
|
+
* @example
|
|
14
|
+
* const limitedFetch = limitAsync(async (url) => {
|
|
15
|
+
* return await fetch(url);
|
|
16
|
+
* }, 3);
|
|
17
|
+
*
|
|
18
|
+
* // Only 3 fetches will run concurrently
|
|
19
|
+
* const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
|
|
20
|
+
* await Promise.all(urls.map(url => limitedFetch(url)));
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const processItem = async (item) => {
|
|
24
|
+
* // Expensive async operation
|
|
25
|
+
* return await heavyComputation(item);
|
|
26
|
+
* };
|
|
27
|
+
*
|
|
28
|
+
* const limitedProcess = limitAsync(processItem, 2);
|
|
29
|
+
* const items = [1, 2, 3, 4, 5];
|
|
30
|
+
* // At most 2 items will be processed concurrently
|
|
31
|
+
* await Promise.all(items.map(item => limitedProcess(item)));
|
|
32
|
+
*/
|
|
33
|
+
declare function limitAsync<F extends (...args: any[]) => Promise<any>>(callback: F, concurrency: number): F;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { limitAsync };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const require_semaphore = require("./semaphore.js");
|
|
2
|
+
//#region src/promise/limitAsync.ts
|
|
3
|
+
/**
|
|
4
|
+
* Wraps an async function to limit the number of concurrent executions.
|
|
5
|
+
*
|
|
6
|
+
* This function creates a wrapper around an async callback that ensures at most
|
|
7
|
+
* `concurrency` number of executions can run simultaneously. Additional calls will
|
|
8
|
+
* wait until a slot becomes available.
|
|
9
|
+
*
|
|
10
|
+
* @template F - The type of the async function to wrap.
|
|
11
|
+
* @param callback The async function to wrap with concurrency control.
|
|
12
|
+
* @param concurrency Maximum number of concurrent executions allowed.
|
|
13
|
+
* @returns A wrapped version of the callback with concurrency limiting.
|
|
14
|
+
* @example
|
|
15
|
+
* const limitedFetch = limitAsync(async (url) => {
|
|
16
|
+
* return await fetch(url);
|
|
17
|
+
* }, 3);
|
|
18
|
+
*
|
|
19
|
+
* // Only 3 fetches will run concurrently
|
|
20
|
+
* const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
|
|
21
|
+
* await Promise.all(urls.map(url => limitedFetch(url)));
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const processItem = async (item) => {
|
|
25
|
+
* // Expensive async operation
|
|
26
|
+
* return await heavyComputation(item);
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* const limitedProcess = limitAsync(processItem, 2);
|
|
30
|
+
* const items = [1, 2, 3, 4, 5];
|
|
31
|
+
* // At most 2 items will be processed concurrently
|
|
32
|
+
* await Promise.all(items.map(item => limitedProcess(item)));
|
|
33
|
+
*/
|
|
34
|
+
function limitAsync(callback, concurrency) {
|
|
35
|
+
const semaphore = new require_semaphore.Semaphore(concurrency);
|
|
36
|
+
return async function(...args) {
|
|
37
|
+
try {
|
|
38
|
+
await semaphore.acquire();
|
|
39
|
+
return await callback.apply(this, args);
|
|
40
|
+
} finally {
|
|
41
|
+
semaphore.release();
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
exports.limitAsync = limitAsync;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Semaphore } from "./semaphore.mjs";
|
|
2
|
+
//#region src/promise/limitAsync.ts
|
|
3
|
+
/**
|
|
4
|
+
* Wraps an async function to limit the number of concurrent executions.
|
|
5
|
+
*
|
|
6
|
+
* This function creates a wrapper around an async callback that ensures at most
|
|
7
|
+
* `concurrency` number of executions can run simultaneously. Additional calls will
|
|
8
|
+
* wait until a slot becomes available.
|
|
9
|
+
*
|
|
10
|
+
* @template F - The type of the async function to wrap.
|
|
11
|
+
* @param callback The async function to wrap with concurrency control.
|
|
12
|
+
* @param concurrency Maximum number of concurrent executions allowed.
|
|
13
|
+
* @returns A wrapped version of the callback with concurrency limiting.
|
|
14
|
+
* @example
|
|
15
|
+
* const limitedFetch = limitAsync(async (url) => {
|
|
16
|
+
* return await fetch(url);
|
|
17
|
+
* }, 3);
|
|
18
|
+
*
|
|
19
|
+
* // Only 3 fetches will run concurrently
|
|
20
|
+
* const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
|
|
21
|
+
* await Promise.all(urls.map(url => limitedFetch(url)));
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const processItem = async (item) => {
|
|
25
|
+
* // Expensive async operation
|
|
26
|
+
* return await heavyComputation(item);
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* const limitedProcess = limitAsync(processItem, 2);
|
|
30
|
+
* const items = [1, 2, 3, 4, 5];
|
|
31
|
+
* // At most 2 items will be processed concurrently
|
|
32
|
+
* await Promise.all(items.map(item => limitedProcess(item)));
|
|
33
|
+
*/
|
|
34
|
+
function limitAsync(callback, concurrency) {
|
|
35
|
+
const semaphore = new Semaphore(concurrency);
|
|
36
|
+
return async function(...args) {
|
|
37
|
+
try {
|
|
38
|
+
await semaphore.acquire();
|
|
39
|
+
return await callback.apply(this, args);
|
|
40
|
+
} finally {
|
|
41
|
+
semaphore.release();
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { limitAsync };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-toolkit",
|
|
3
|
-
"version": "1.50.0-dev.
|
|
3
|
+
"version": "1.50.0-dev.2050+ad4815bf",
|
|
4
4
|
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
|
|
5
5
|
"homepage": "https://es-toolkit.dev",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|