@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 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
@@ -0,0 +1,4 @@
1
+ {
2
+ "semi": false,
3
+ "singleQuote": true
4
+ }
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @chriscdn/promise-retry
2
2
 
3
- Retry a function returning a rejected promise until it resolves or exceeds the maximum attempt count.
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
- ``` js
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, 10)
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
- function promiseRetry(func, maxAttempts = 10, attempt = 1) {
4
- return func(attempt)
5
- .catch(err => {
6
- if (attempt < maxAttempts) {
7
- return promiseRetry(func, maxAttempts, attempt + 1)
8
- } else {
9
- throw err
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
- function promiseRetry(func, maxAttempts = 10, attempt = 1) {
2
- return func(attempt)
3
- .catch(err => {
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,
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 src;
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.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
- "private": false,
10
+ "scripts": {
11
+ "build": "rollup -c",
12
+ "watch": "rollup -c --watch"
13
+ },
11
14
  "devDependencies": {
12
- "@rollup/plugin-commonjs": "^11.0.2"
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
- input: 'src/index.js',
6
- output: [{
7
- file: pkg.main,
8
- format: 'cjs'
9
- }],
10
-
11
- plugins: [
12
- // commonjs()
13
- ]
14
- }, {
15
- input: 'src/index.js',
16
- output: [{
17
- file: pkg.module,
18
- format: 'es'
19
- }],
20
- plugins: [
21
- commonjs()
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
- function promiseRetry(func, maxAttempts = 10, attempt = 1) {
2
- return func(attempt)
3
- .catch(err => {
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
- module.exports = promiseRetry
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