retry-utils-testmetest 0.0.0-stage → 1.0.0
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/README.md +6 -3
- package/package.json +7 -4
- package/src/index.js +14 -0
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# retry-utils-testmetest
|
|
2
|
+
Retry utility with exponential backoff.
|
|
3
|
+
```js
|
|
4
|
+
const { retry } = require('retry-utils-testmetest');
|
|
5
|
+
const result = await retry(() => fetch('https://api.example.com/data'), { attempts: 3 });
|
|
6
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "retry-utils-testmetest",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Retry utility functions with exponential backoff",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"keywords": ["retry", "backoff", "utils", "async"],
|
|
7
|
+
"author": "alexjoipamiba",
|
|
8
|
+
"license": "MIT"
|
|
9
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
async function retry(fn, opts = {}) {
|
|
2
|
+
const { attempts = 3, delay = 1000, factor = 2 } = opts;
|
|
3
|
+
let lastErr;
|
|
4
|
+
for (let i = 0; i < attempts; i++) {
|
|
5
|
+
try { return await fn(); }
|
|
6
|
+
catch (e) {
|
|
7
|
+
lastErr = e;
|
|
8
|
+
if (i < attempts - 1)
|
|
9
|
+
await new Promise(r => setTimeout(r, delay * Math.pow(factor, i)));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
throw lastErr;
|
|
13
|
+
}
|
|
14
|
+
module.exports = { retry };
|