@chriscdn/promise-retry 1.0.1 → 1.0.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/.prettierrc +4 -0
- package/README.md +40 -26
- package/lib/index.cjs.js +15 -15
- package/lib/index.es.js +16 -16
- package/package.json +19 -19
- package/rollup.config.js +21 -16
- package/src/index.js +16 -16
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
|
|
|
@@ -16,42 +16,56 @@ Using yarn:
|
|
|
16
16
|
$ yarn add @chriscdn/promise-retry
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
## Example
|
|
19
|
+
## Example 1 - Promises
|
|
20
20
|
|
|
21
21
|
```js
|
|
22
|
-
const promiseRetry = require('@chriscdn/promise-
|
|
22
|
+
const promiseRetry = require('@chriscdn/promise-retry')
|
|
23
23
|
|
|
24
24
|
function myFunction(attempt) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
})
|
|
35
|
-
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
// ... do something
|
|
27
|
+
|
|
28
|
+
if (allIsFine) {
|
|
29
|
+
resolve(/* <value> */)
|
|
30
|
+
} else {
|
|
31
|
+
reject(/* <err> */)
|
|
32
|
+
}
|
|
33
|
+
})
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
const options = {
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
maxAttempts: 10,
|
|
38
|
+
retryDelay: 0,
|
|
41
39
|
}
|
|
42
40
|
|
|
43
41
|
// Call myFunction until a resolved promise is returned, but not more than 10 times (default is 10)
|
|
44
|
-
promiseRetry(attempt => myFunction(attempt), options)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
42
|
+
promiseRetry((attempt) => myFunction(attempt), options)
|
|
43
|
+
.then((value) => {
|
|
44
|
+
// myFunction resolved within 10 attempts
|
|
45
|
+
// value is from the myFunction resolve call
|
|
46
|
+
})
|
|
47
|
+
.catch((err) => {
|
|
48
|
+
// myFunction failed to return a resolved promise within 10 attempts
|
|
49
|
+
// err is the reject value from the last attempt
|
|
50
|
+
})
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Example 2 - Async/Await
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
const promiseRetry = require('@chriscdn/promise-retry')
|
|
57
|
+
|
|
58
|
+
const results = await promiseRetry(
|
|
59
|
+
async (attempt) => {
|
|
60
|
+
// do something async in here
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
maxAttempts: 10,
|
|
64
|
+
retryDelay: 0,
|
|
65
|
+
}
|
|
66
|
+
)
|
|
53
67
|
```
|
|
54
68
|
|
|
55
69
|
## License
|
|
56
70
|
|
|
57
|
-
[MIT](LICENSE)
|
|
71
|
+
[MIT](LICENSE)
|
package/lib/index.cjs.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const defaultOptions = {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
maxAttempts: 10,
|
|
5
|
+
retryDelay: 0,
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
function promiseRetry(func, options = defaultOptions, attempt = 1) {
|
|
9
|
+
const config = { ...defaultOptions, ...options };
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
})
|
|
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.retryDelay
|
|
17
|
+
);
|
|
18
|
+
})
|
|
19
|
+
} else {
|
|
20
|
+
throw err
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
module.exports = promiseRetry;
|
package/lib/index.es.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
maxAttempts: 10,
|
|
3
|
+
retryDelay: 0,
|
|
4
4
|
};
|
|
5
5
|
|
|
6
6
|
function promiseRetry(func, options = defaultOptions, attempt = 1) {
|
|
7
|
+
const config = { ...defaultOptions, ...options };
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
})
|
|
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.retryDelay
|
|
15
|
+
);
|
|
16
|
+
})
|
|
17
|
+
} else {
|
|
18
|
+
throw err
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
var src = promiseRetry;
|
|
24
24
|
|
|
25
|
-
export default
|
|
25
|
+
export { src as default };
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
"name": "@chriscdn/promise-retry",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Retry a function returning a rejected promise until it resolves.",
|
|
5
|
+
"main": "lib/index.cjs.js",
|
|
6
|
+
"module": "lib/index.es.js",
|
|
7
|
+
"repository": "https://github.com/chriscdn/promise-retry",
|
|
8
|
+
"author": "Christopher Meyer <chris@schwiiz.org>",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "rollup -c",
|
|
12
|
+
"watch": "rollup -c --watch"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@rollup/plugin-commonjs": "^21.0.3"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"promise",
|
|
19
|
+
"retry"
|
|
20
|
+
]
|
|
21
21
|
}
|
package/rollup.config.js
CHANGED
|
@@ -1,19 +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
|
-
|
|
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,23 +1,23 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
maxAttempts: 10,
|
|
3
|
+
retryDelay: 0,
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
function promiseRetry(func, options = defaultOptions, attempt = 1) {
|
|
7
|
+
const config = { ...defaultOptions, ...options }
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
})
|
|
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.retryDelay
|
|
15
|
+
)
|
|
16
|
+
})
|
|
17
|
+
} else {
|
|
18
|
+
throw err
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
module.exports = promiseRetry
|
|
23
|
+
module.exports = promiseRetry
|