@orion-js/helpers 2.10.0 → 2.12.4
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/executeWithRetries.js +46 -0
- package/lib/index.js +9 -1
- package/package.json +2 -2
- package/LICENSE +0 -21
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = executeWithRetries;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Executes an asynchronous function with automatic retries on failure.
|
|
10
|
+
*
|
|
11
|
+
* This utility attempts to execute the provided function and automatically
|
|
12
|
+
* retries if it fails, with a specified delay between attempts. It will
|
|
13
|
+
* continue retrying until either the function succeeds or the maximum
|
|
14
|
+
* number of retries is reached.
|
|
15
|
+
*
|
|
16
|
+
* @param {() => Promise<any>} fn - The asynchronous function to execute
|
|
17
|
+
* @param {number} retries - The maximum number of retry attempts after the initial attempt
|
|
18
|
+
* @param {number} timeout - The delay in milliseconds between retry attempts
|
|
19
|
+
* @returns {Promise<any>} A promise that resolves with the result of the function or rejects with the last error
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Retry an API call up to 3 times with 1 second between attempts
|
|
23
|
+
* const result = await executeWithRetries(
|
|
24
|
+
* () => fetchDataFromApi(),
|
|
25
|
+
* 3,
|
|
26
|
+
* 1000
|
|
27
|
+
* )
|
|
28
|
+
*/
|
|
29
|
+
function executeWithRetries(fn, retries, timeout) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const retry = async remaining => {
|
|
32
|
+
try {
|
|
33
|
+
const result = await fn();
|
|
34
|
+
resolve(result);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (remaining > 0) {
|
|
37
|
+
setTimeout(() => retry(remaining - 1), timeout);
|
|
38
|
+
} else {
|
|
39
|
+
reject(error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
retry(retries);
|
|
45
|
+
});
|
|
46
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -29,6 +29,12 @@ Object.defineProperty(exports, "createMap", {
|
|
|
29
29
|
return _createMap.default;
|
|
30
30
|
}
|
|
31
31
|
});
|
|
32
|
+
Object.defineProperty(exports, "executeWithRetries", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _executeWithRetries.default;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
32
38
|
|
|
33
39
|
var _sleep = _interopRequireDefault(require("./sleep"));
|
|
34
40
|
|
|
@@ -36,4 +42,6 @@ var _hashObject = _interopRequireDefault(require("./hashObject"));
|
|
|
36
42
|
|
|
37
43
|
var _generateId = _interopRequireDefault(require("./generateId"));
|
|
38
44
|
|
|
39
|
-
var _createMap = _interopRequireDefault(require("./createMap"));
|
|
45
|
+
var _createMap = _interopRequireDefault(require("./createMap"));
|
|
46
|
+
|
|
47
|
+
var _executeWithRetries = _interopRequireDefault(require("./executeWithRetries"));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/helpers",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12.4",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"author": "nicolaslopezj",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "cb12e3b5929d63ea486c44853dd1c9040a91dfb9"
|
|
30
30
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2018 Orionjs Team
|
|
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.
|