@chriscdn/promise-retry 1.0.0 → 1.0.3
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/.eslintrc.js +20 -0
- package/.prettierrc +4 -0
- package/README.md +8 -3
- package/lib/index.cjs.js +20 -9
- package/lib/index.es.js +21 -10
- package/package.json +11 -4
- package/rollup.config.js +21 -20
- package/src/index.js +21 -10
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
env: {
|
|
3
|
+
commonjs: true,
|
|
4
|
+
es2021: true,
|
|
5
|
+
node: true,
|
|
6
|
+
},
|
|
7
|
+
extends: 'eslint:recommended',
|
|
8
|
+
parserOptions: {
|
|
9
|
+
ecmaVersion: 'latest',
|
|
10
|
+
},
|
|
11
|
+
rules: {
|
|
12
|
+
'no-unused-vars': [
|
|
13
|
+
'error',
|
|
14
|
+
{
|
|
15
|
+
argsIgnorePattern: '^_',
|
|
16
|
+
varsIgnorePattern: '^_',
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
}
|
package/.prettierrc
ADDED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @chriscdn/promise-retry
|
|
2
2
|
|
|
3
|
-
Retry a function returning a
|
|
3
|
+
Retry a function returning a promise until it resolves successfully or exceeds the maximum attempt count.
|
|
4
4
|
|
|
5
5
|
## Installing
|
|
6
6
|
|
|
@@ -18,7 +18,7 @@ $ yarn add @chriscdn/promise-retry
|
|
|
18
18
|
|
|
19
19
|
## Example
|
|
20
20
|
|
|
21
|
-
```
|
|
21
|
+
```js
|
|
22
22
|
const promiseRetry = require('@chriscdn/promise-reject')
|
|
23
23
|
|
|
24
24
|
function myFunction(attempt) {
|
|
@@ -35,8 +35,13 @@ function myFunction(attempt) {
|
|
|
35
35
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
const options = {
|
|
39
|
+
maxAttempts: 10,
|
|
40
|
+
timeout: 0
|
|
41
|
+
}
|
|
42
|
+
|
|
38
43
|
// Call myFunction until a resolved promise is returned, but not more than 10 times (default is 10)
|
|
39
|
-
promiseRetry(myFunction,
|
|
44
|
+
promiseRetry(attempt => myFunction(attempt), options)
|
|
40
45
|
.then(value => {
|
|
41
46
|
// myFunction resolved within 10 attempts
|
|
42
47
|
// value is from the myFunction resolve call
|
package/lib/index.cjs.js
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
const defaultOptions = {
|
|
4
|
+
maxAttempts: 10,
|
|
5
|
+
milliseconds: 0,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
function promiseRetry(func, options = defaultOptions, attempt = 1) {
|
|
9
|
+
const config = { ...defaultOptions, ...options };
|
|
10
|
+
|
|
11
|
+
return func(attempt).catch((err) => {
|
|
12
|
+
if (attempt < config.maxAttempts) {
|
|
13
|
+
return new Promise((resolve) => {
|
|
14
|
+
setTimeout(
|
|
15
|
+
() => resolve(promiseRetry(func, options, attempt + 1)),
|
|
16
|
+
config.milliseconds
|
|
17
|
+
);
|
|
18
|
+
})
|
|
19
|
+
} else {
|
|
20
|
+
throw err
|
|
21
|
+
}
|
|
22
|
+
})
|
|
12
23
|
}
|
|
13
24
|
|
|
14
25
|
module.exports = promiseRetry;
|
package/lib/index.es.js
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
const defaultOptions = {
|
|
2
|
+
maxAttempts: 10,
|
|
3
|
+
milliseconds: 0,
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
function promiseRetry(func, options = defaultOptions, attempt = 1) {
|
|
7
|
+
const config = { ...defaultOptions, ...options };
|
|
8
|
+
|
|
9
|
+
return func(attempt).catch((err) => {
|
|
10
|
+
if (attempt < config.maxAttempts) {
|
|
11
|
+
return new Promise((resolve) => {
|
|
12
|
+
setTimeout(
|
|
13
|
+
() => resolve(promiseRetry(func, options, attempt + 1)),
|
|
14
|
+
config.milliseconds
|
|
15
|
+
);
|
|
16
|
+
})
|
|
17
|
+
} else {
|
|
18
|
+
throw err
|
|
19
|
+
}
|
|
20
|
+
})
|
|
10
21
|
}
|
|
11
22
|
|
|
12
23
|
var src = promiseRetry;
|
|
13
24
|
|
|
14
|
-
export default
|
|
25
|
+
export { src as default };
|
package/package.json
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chriscdn/promise-retry",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Retry a function returning a rejected promise until it resolves.",
|
|
5
5
|
"main": "lib/index.cjs.js",
|
|
6
6
|
"module": "lib/index.es.js",
|
|
7
7
|
"repository": "https://github.com/chriscdn/promise-retry",
|
|
8
8
|
"author": "Christopher Meyer <chris@schwiiz.org>",
|
|
9
9
|
"license": "MIT",
|
|
10
|
-
"
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "rollup -c",
|
|
12
|
+
"watch": "rollup -c --watch"
|
|
13
|
+
},
|
|
11
14
|
"devDependencies": {
|
|
12
|
-
"@rollup/plugin-commonjs": "^
|
|
13
|
-
}
|
|
15
|
+
"@rollup/plugin-commonjs": "^21.0.2"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"promise",
|
|
19
|
+
"retry"
|
|
20
|
+
]
|
|
14
21
|
}
|
package/rollup.config.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import pkg from './package.json'
|
|
2
2
|
import commonjs from '@rollup/plugin-commonjs'
|
|
3
3
|
|
|
4
|
-
export default [
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
4
|
+
export default [
|
|
5
|
+
{
|
|
6
|
+
input: 'src/index.js',
|
|
7
|
+
output: [
|
|
8
|
+
{
|
|
9
|
+
file: pkg.main,
|
|
10
|
+
format: 'cjs',
|
|
11
|
+
},
|
|
12
|
+
],
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
input: 'src/index.js',
|
|
16
|
+
output: [
|
|
17
|
+
{
|
|
18
|
+
file: pkg.module,
|
|
19
|
+
format: 'es',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
plugins: [commonjs()],
|
|
23
|
+
},
|
|
24
|
+
]
|
package/src/index.js
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (attempt < maxAttempts) {
|
|
5
|
-
return promiseRetry(func, maxAttempts, attempt + 1)
|
|
6
|
-
} else {
|
|
7
|
-
throw err
|
|
8
|
-
}
|
|
9
|
-
})
|
|
1
|
+
const defaultOptions = {
|
|
2
|
+
maxAttempts: 10,
|
|
3
|
+
milliseconds: 0,
|
|
10
4
|
}
|
|
11
5
|
|
|
12
|
-
|
|
6
|
+
function promiseRetry(func, options = defaultOptions, attempt = 1) {
|
|
7
|
+
const config = { ...defaultOptions, ...options }
|
|
8
|
+
|
|
9
|
+
return func(attempt).catch((err) => {
|
|
10
|
+
if (attempt < config.maxAttempts) {
|
|
11
|
+
return new Promise((resolve) => {
|
|
12
|
+
setTimeout(
|
|
13
|
+
() => resolve(promiseRetry(func, options, attempt + 1)),
|
|
14
|
+
config.milliseconds
|
|
15
|
+
)
|
|
16
|
+
})
|
|
17
|
+
} else {
|
|
18
|
+
throw err
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = promiseRetry
|