@contrail/util 1.0.57 → 1.0.58
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/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -20,3 +20,4 @@ __exportStar(require("./date-util/date-util"), exports);
|
|
|
20
20
|
__exportStar(require("./map-util/map-util"), exports);
|
|
21
21
|
__exportStar(require("./app-util/app-util"), exports);
|
|
22
22
|
__exportStar(require("./timer-util/timer-util"), exports);
|
|
23
|
+
__exportStar(require("./promise-util/promise-util"), exports);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare class PromiseUtil {
|
|
2
|
+
static resolveInChunks(objects: Array<any>, chunksSize: number, asyncFunction: (chunk: any) => Promise<any>): Promise<any>;
|
|
3
|
+
static resolveInChunksAndLimit(objects: Array<any>, chunksSize: number, limitSize: number, asyncFunction: (chunk: any) => Promise<any>): Promise<any>;
|
|
4
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PromiseUtil = void 0;
|
|
13
|
+
const p_limit_1 = require("p-limit");
|
|
14
|
+
const lodash_1 = require("lodash");
|
|
15
|
+
class PromiseUtil {
|
|
16
|
+
static resolveInChunks(objects, chunksSize, asyncFunction) {
|
|
17
|
+
const chunks = (0, lodash_1.chunk)(objects, chunksSize);
|
|
18
|
+
const promises = [];
|
|
19
|
+
for (const chunk of chunks) {
|
|
20
|
+
promises.push(asyncFunction(chunk));
|
|
21
|
+
}
|
|
22
|
+
return Promise.all(promises).then((chunkedResults) => chunkedResults.flat());
|
|
23
|
+
}
|
|
24
|
+
static resolveInChunksAndLimit(objects, chunksSize, limitSize, asyncFunction) {
|
|
25
|
+
const limit = (0, p_limit_1.default)(limitSize);
|
|
26
|
+
const chunks = (0, lodash_1.chunk)(objects, chunksSize);
|
|
27
|
+
const promises = [];
|
|
28
|
+
for (const chunk of chunks) {
|
|
29
|
+
const promise = limit(() => __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
return yield asyncFunction(chunk);
|
|
31
|
+
}));
|
|
32
|
+
promises.push(promise);
|
|
33
|
+
}
|
|
34
|
+
return Promise.all(promises).then((chunkedResults) => chunkedResults.flat());
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.PromiseUtil = PromiseUtil;
|