@contrail/util 1.1.0 → 1.1.1-1
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 +1 -0
- package/lib/index.js +1 -0
- package/lib/retry-util/retry-util.d.ts +6 -0
- package/lib/retry-util/retry-util.js +42 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -22,3 +22,4 @@ __exportStar(require("./app-util/app-util"), exports);
|
|
|
22
22
|
__exportStar(require("./timer-util/timer-util"), exports);
|
|
23
23
|
__exportStar(require("./promise-util/promise-util"), exports);
|
|
24
24
|
__exportStar(require("./order-util/order-util"), exports);
|
|
25
|
+
__exportStar(require("./retry-util/retry-util"), exports);
|
|
@@ -0,0 +1,42 @@
|
|
|
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.retry = void 0;
|
|
13
|
+
const DEFAULT_CONFIG = {
|
|
14
|
+
maxRetries: 3,
|
|
15
|
+
initialDelay: 1000,
|
|
16
|
+
};
|
|
17
|
+
function retry(fn, config = DEFAULT_CONFIG) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
const { maxRetries, initialDelay, onRetryCallback } = Object.assign(Object.assign({}, DEFAULT_CONFIG), config);
|
|
20
|
+
if (maxRetries < 1) {
|
|
21
|
+
throw new Error('maxRetries must be at least 1');
|
|
22
|
+
}
|
|
23
|
+
let attempt = 0;
|
|
24
|
+
while (attempt < maxRetries) {
|
|
25
|
+
try {
|
|
26
|
+
return yield fn();
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
attempt++;
|
|
30
|
+
if (attempt === maxRetries) {
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
if (onRetryCallback) {
|
|
34
|
+
yield onRetryCallback(error, attempt);
|
|
35
|
+
}
|
|
36
|
+
yield new Promise((resolve) => setTimeout(resolve, initialDelay * attempt));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new Error('Max retries reached');
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
exports.retry = retry;
|