@chriscdn/promise-retry 2.0.7 → 3.0.1
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/LICENSE +1 -1
- package/README.md +42 -38
- package/lib/index.d.ts +4 -3
- package/lib/promise-retry.cjs +1 -1
- package/lib/promise-retry.cjs.map +1 -1
- package/lib/promise-retry.modern.js +1 -1
- package/lib/promise-retry.modern.js.map +1 -1
- package/lib/promise-retry.module.js +1 -1
- package/lib/promise-retry.module.js.map +1 -1
- package/lib/promise-retry.umd.js +1 -1
- package/lib/promise-retry.umd.js.map +1 -1
- package/package.json +8 -3
- package/src/index.ts +0 -37
- package/src/test.js +0 -41
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -16,58 +16,62 @@ Using yarn:
|
|
|
16
16
|
yarn add @chriscdn/promise-retry
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
##
|
|
19
|
+
## Upgrading to v3
|
|
20
|
+
|
|
21
|
+
The import has changed from
|
|
20
22
|
|
|
21
23
|
```js
|
|
22
24
|
import promiseRetry from "@chriscdn/promise-retry";
|
|
25
|
+
```
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
// ... do something
|
|
27
|
+
to
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
});
|
|
34
|
-
}
|
|
29
|
+
```js
|
|
30
|
+
import { promiseRetry } from "@chriscdn/promise-retry";
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Example 1 - Async/Await
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
```js
|
|
36
|
+
import { promiseRetry, type RetryOptions } from "@chriscdn/promise-retry";
|
|
37
|
+
|
|
38
|
+
// all options are optional, defaults below
|
|
39
|
+
const options: RetryOptions = {
|
|
40
|
+
maxAttempts: 10, // Maximum retry attempts (default: 10)
|
|
41
|
+
retryDelay: 0, // Delay between retries (in ms)
|
|
42
|
+
onError: (err, attempt) => {
|
|
43
|
+
// log the error
|
|
44
|
+
},
|
|
40
45
|
};
|
|
41
46
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
// myFunction resolved within 10 attempts
|
|
46
|
-
// value is from the myFunction resolve call
|
|
47
|
-
})
|
|
48
|
-
.catch((err) => {
|
|
49
|
-
// myFunction failed to return a resolved promise within 10 attempts
|
|
50
|
-
// err is the reject value from the last attempt
|
|
51
|
-
});
|
|
47
|
+
const results = await promiseRetry(async (attempt) => {
|
|
48
|
+
// do something async in here
|
|
49
|
+
}, options);
|
|
52
50
|
```
|
|
53
51
|
|
|
54
|
-
## Example 2 -
|
|
52
|
+
## Example 2 - Retryify
|
|
53
|
+
|
|
54
|
+
`Retryify` wraps an asynchronous function and returns a new function with the same interface. If the original function fails (i.e., rejects its promise), it will automatically retry the function up to the specified number of times before rejecting.
|
|
55
55
|
|
|
56
56
|
```js
|
|
57
|
-
import
|
|
57
|
+
import { Retryify } from "@chriscdn/promise-retry";
|
|
58
58
|
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
maxAttempts: 10,
|
|
65
|
-
retryDelay: 0,
|
|
66
|
-
onError: (err, attempt) => {
|
|
67
|
-
// log the error
|
|
68
|
-
},
|
|
59
|
+
const myAsyncFunctionRandomFails = async (a, b) => {
|
|
60
|
+
if (Math.random() < 0.2) {
|
|
61
|
+
return a + b;
|
|
62
|
+
} else {
|
|
63
|
+
throw new Error("Random failure");
|
|
69
64
|
}
|
|
70
|
-
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const myAsyncFunctionRetry = Retryify(myAsyncFunctionRandomFails, options);
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const sum = await myAsyncFunctionRetry(1, 5);
|
|
71
|
+
console.log("Success: ", sum);
|
|
72
|
+
} catch (err) {
|
|
73
|
+
console.error("Failed after retries:", err);
|
|
74
|
+
}
|
|
71
75
|
```
|
|
72
76
|
|
|
73
77
|
## License
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
type
|
|
1
|
+
type RetryOptions = {
|
|
2
2
|
maxAttempts: number;
|
|
3
3
|
retryDelay: number;
|
|
4
4
|
onError: (err: any, attempt: number) => void;
|
|
5
5
|
};
|
|
6
|
-
declare const promiseRetry: <T>(func: (attempt: number) => Promise<T>, options?: Partial<
|
|
7
|
-
|
|
6
|
+
declare const promiseRetry: <T>(func: (attempt: number) => Promise<T>, options?: Partial<RetryOptions>, attempt?: number) => Promise<T>;
|
|
7
|
+
declare const Retryify: <Args extends unknown[], Return>(cb: (...args: Args) => Promise<Return>, options?: Partial<RetryOptions>) => (...args: Args) => Promise<Return>;
|
|
8
|
+
export { promiseRetry, Retryify, type RetryOptions };
|
package/lib/promise-retry.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function r(){return r=Object.assign?Object.assign.bind():function(r){for(var
|
|
1
|
+
function r(){return r=Object.assign?Object.assign.bind():function(r){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var e in t)({}).hasOwnProperty.call(t,e)&&(r[e]=t[e])}return r},r.apply(null,arguments)}var n={maxAttempts:10,retryDelay:0,onError:function(r,n){}},t=function(e,o,i){void 0===o&&(o=n),void 0===i&&(i=1);var u=r({},n,o);return e(i).catch(function(r){if(u.onError(r,i),i<u.maxAttempts)return new Promise(function(r){setTimeout(function(){return r(t(e,o,i+1))},u.retryDelay)});throw r})};exports.Retryify=function(r,n){return void 0===n&&(n={}),function(){var e=arguments;return t(function(){return r.apply(void 0,[].slice.call(e))},n)}},exports.promiseRetry=t;
|
|
2
2
|
//# sourceMappingURL=promise-retry.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-retry.cjs","sources":["../src/index.ts"],"sourcesContent":["type
|
|
1
|
+
{"version":3,"file":"promise-retry.cjs","sources":["../src/index.ts"],"sourcesContent":["type RetryOptions = {\n maxAttempts: number;\n retryDelay: number;\n onError: (err: any, attempt: number) => void;\n};\n\nconst defaultOptions: RetryOptions = {\n maxAttempts: 10,\n retryDelay: 0,\n onError: (err: any, attempt: number) => {},\n};\n\nconst promiseRetry = <T>(\n func: (attempt: number) => Promise<T>,\n options: Partial<RetryOptions> = defaultOptions,\n attempt = 1,\n): Promise<T> => {\n const config: RetryOptions = { ...defaultOptions, ...options };\n\n return func(attempt).catch((err: any) => {\n // For logging...\n config.onError(err, attempt);\n\n if (attempt < config.maxAttempts) {\n return new Promise((resolve) => {\n setTimeout(\n () => resolve(promiseRetry(func, options, attempt + 1)),\n config.retryDelay,\n );\n });\n } else {\n throw err;\n }\n });\n};\n\nconst Retryify = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<RetryOptions> = {},\n) =>\n(...args: Args): Promise<Return> => {\n return promiseRetry<Return>(() => {\n return cb(...args);\n }, options);\n};\n\nexport { promiseRetry, Retryify, type RetryOptions };\n"],"names":["defaultOptions","maxAttempts","retryDelay","onError","err","attempt","promiseRetry","func","options","config","_extends","Promise","resolve","setTimeout","cb","_arguments","arguments","apply","slice","call"],"mappings":"wNAMA,IAAMA,EAA+B,CACnCC,YAAa,GACbC,WAAY,EACZC,QAAS,SAACC,EAAUC,MAGhBC,EAAe,SACnBC,EACAC,EACAH,QADA,IAAAG,IAAAA,EAAiCR,QACjCK,IAAAA,IAAAA,EAAU,GAEV,IAAMI,EAAMC,EAAA,GAAsBV,EAAmBQ,GAErD,OAAOD,EAAKF,GAAQ,MAAO,SAACD,GAI1B,GAFAK,EAAON,QAAQC,EAAKC,GAEhBA,EAAUI,EAAOR,YACnB,OAAW,IAAAU,QAAQ,SAACC,GAClBC,WACE,WAAA,OAAMD,EAAQN,EAAaC,EAAMC,EAASH,EAAU,GAAG,EACvDI,EAAOP,WAEX,GAEA,MAAME,CAEV,EACF,mBAEiB,SACfU,EACAN,GAEF,YAFmC,IAAjCA,IAAAA,EAAiC,CAAE,GAErC,eAAmCO,EAAAC,UACjC,OAAOV,EAAqB,WAC1B,OAAOQ,EAAEG,gBAAAC,MAAAC,KAAAJ,GACX,EAAGP,EACL,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function
|
|
1
|
+
function r(){return r=Object.assign?Object.assign.bind():function(r){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var e in n)({}).hasOwnProperty.call(n,e)&&(r[e]=n[e])}return r},r.apply(null,arguments)}const t={maxAttempts:10,retryDelay:0,onError:(r,t)=>{}},n=(e,o=t,a=1)=>{const s=r({},t,o);return e(a).catch(r=>{if(s.onError(r,a),a<s.maxAttempts)return new Promise(r=>{setTimeout(()=>r(n(e,o,a+1)),s.retryDelay)});throw r})},e=(r,t={})=>(...e)=>n(()=>r(...e),t);export{e as Retryify,n as promiseRetry};
|
|
2
2
|
//# sourceMappingURL=promise-retry.modern.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-retry.modern.js","sources":["../src/index.ts"],"sourcesContent":["type
|
|
1
|
+
{"version":3,"file":"promise-retry.modern.js","sources":["../src/index.ts"],"sourcesContent":["type RetryOptions = {\n maxAttempts: number;\n retryDelay: number;\n onError: (err: any, attempt: number) => void;\n};\n\nconst defaultOptions: RetryOptions = {\n maxAttempts: 10,\n retryDelay: 0,\n onError: (err: any, attempt: number) => {},\n};\n\nconst promiseRetry = <T>(\n func: (attempt: number) => Promise<T>,\n options: Partial<RetryOptions> = defaultOptions,\n attempt = 1,\n): Promise<T> => {\n const config: RetryOptions = { ...defaultOptions, ...options };\n\n return func(attempt).catch((err: any) => {\n // For logging...\n config.onError(err, attempt);\n\n if (attempt < config.maxAttempts) {\n return new Promise((resolve) => {\n setTimeout(\n () => resolve(promiseRetry(func, options, attempt + 1)),\n config.retryDelay,\n );\n });\n } else {\n throw err;\n }\n });\n};\n\nconst Retryify = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<RetryOptions> = {},\n) =>\n(...args: Args): Promise<Return> => {\n return promiseRetry<Return>(() => {\n return cb(...args);\n }, options);\n};\n\nexport { promiseRetry, Retryify, type RetryOptions };\n"],"names":["defaultOptions","maxAttempts","retryDelay","onError","err","attempt","promiseRetry","func","options","config","_extends","catch","Promise","resolve","setTimeout","Retryify","cb","args"],"mappings":"wNAMA,MAAMA,EAA+B,CACnCC,YAAa,GACbC,WAAY,EACZC,QAASA,CAACC,EAAUC,QAGhBC,EAAeA,CACnBC,EACAC,EAAiCR,EACjCK,EAAU,KAEV,MAAMI,EAAMC,EAAsBV,GAAAA,EAAmBQ,GAErD,OAAOD,EAAKF,GAASM,MAAOP,IAI1B,GAFAK,EAAON,QAAQC,EAAKC,GAEhBA,EAAUI,EAAOR,YACnB,OAAW,IAAAW,QAASC,IAClBC,WACE,IAAMD,EAAQP,EAAaC,EAAMC,EAASH,EAAU,IACpDI,EAAOP,cAIX,MAAME,KAKNW,EAAWA,CACfC,EACAR,EAAiC,CAAA,IAEnC,IAAIS,IACKX,EAAqB,IACnBU,KAAMC,GACZT"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function
|
|
1
|
+
function r(){return r=Object.assign?Object.assign.bind():function(r){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var e in t)({}).hasOwnProperty.call(t,e)&&(r[e]=t[e])}return r},r.apply(null,arguments)}var n={maxAttempts:10,retryDelay:0,onError:function(r,n){}},t=function(e,o,i){void 0===o&&(o=n),void 0===i&&(i=1);var u=r({},n,o);return e(i).catch(function(r){if(u.onError(r,i),i<u.maxAttempts)return new Promise(function(r){setTimeout(function(){return r(t(e,o,i+1))},u.retryDelay)});throw r})},e=function(r,n){return void 0===n&&(n={}),function(){var e=arguments;return t(function(){return r.apply(void 0,[].slice.call(e))},n)}};export{e as Retryify,t as promiseRetry};
|
|
2
2
|
//# sourceMappingURL=promise-retry.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-retry.module.js","sources":["../src/index.ts"],"sourcesContent":["type
|
|
1
|
+
{"version":3,"file":"promise-retry.module.js","sources":["../src/index.ts"],"sourcesContent":["type RetryOptions = {\n maxAttempts: number;\n retryDelay: number;\n onError: (err: any, attempt: number) => void;\n};\n\nconst defaultOptions: RetryOptions = {\n maxAttempts: 10,\n retryDelay: 0,\n onError: (err: any, attempt: number) => {},\n};\n\nconst promiseRetry = <T>(\n func: (attempt: number) => Promise<T>,\n options: Partial<RetryOptions> = defaultOptions,\n attempt = 1,\n): Promise<T> => {\n const config: RetryOptions = { ...defaultOptions, ...options };\n\n return func(attempt).catch((err: any) => {\n // For logging...\n config.onError(err, attempt);\n\n if (attempt < config.maxAttempts) {\n return new Promise((resolve) => {\n setTimeout(\n () => resolve(promiseRetry(func, options, attempt + 1)),\n config.retryDelay,\n );\n });\n } else {\n throw err;\n }\n });\n};\n\nconst Retryify = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<RetryOptions> = {},\n) =>\n(...args: Args): Promise<Return> => {\n return promiseRetry<Return>(() => {\n return cb(...args);\n }, options);\n};\n\nexport { promiseRetry, Retryify, type RetryOptions };\n"],"names":["defaultOptions","maxAttempts","retryDelay","onError","err","attempt","promiseRetry","func","options","config","_extends","Promise","resolve","setTimeout","Retryify","cb","_arguments","arguments","apply","slice","call"],"mappings":"wNAMA,IAAMA,EAA+B,CACnCC,YAAa,GACbC,WAAY,EACZC,QAAS,SAACC,EAAUC,MAGhBC,EAAe,SACnBC,EACAC,EACAH,QADA,IAAAG,IAAAA,EAAiCR,QACjCK,IAAAA,IAAAA,EAAU,GAEV,IAAMI,EAAMC,EAAA,GAAsBV,EAAmBQ,GAErD,OAAOD,EAAKF,GAAQ,MAAO,SAACD,GAI1B,GAFAK,EAAON,QAAQC,EAAKC,GAEhBA,EAAUI,EAAOR,YACnB,OAAW,IAAAU,QAAQ,SAACC,GAClBC,WACE,WAAA,OAAMD,EAAQN,EAAaC,EAAMC,EAASH,EAAU,GAAG,EACvDI,EAAOP,WAEX,GAEA,MAAME,CAEV,EACF,EAEMU,EAAW,SACfC,EACAP,GAEF,YAFmC,IAAjCA,IAAAA,EAAiC,CAAE,GAErC,eAAmCQ,EAAAC,UACjC,OAAOX,EAAqB,WAC1B,OAAOS,EAAEG,gBAAAC,MAAAC,KAAAJ,GACX,EAAGR,EACL,CAAC"}
|
package/lib/promise-retry.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(t
|
|
1
|
+
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n||self).promiseRetry={})}(this,function(n){function t(){return t=Object.assign?Object.assign.bind():function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var r in e)({}).hasOwnProperty.call(e,r)&&(n[r]=e[r])}return n},t.apply(null,arguments)}var e={maxAttempts:10,retryDelay:0,onError:function(n,t){}},r=function(n,o,i){void 0===o&&(o=e),void 0===i&&(i=1);var f=t({},e,o);return n(i).catch(function(t){if(f.onError(t,i),i<f.maxAttempts)return new Promise(function(t){setTimeout(function(){return t(r(n,o,i+1))},f.retryDelay)});throw t})};n.Retryify=function(n,t){return void 0===t&&(t={}),function(){var e=arguments;return r(function(){return n.apply(void 0,[].slice.call(e))},t)}},n.promiseRetry=r});
|
|
2
2
|
//# sourceMappingURL=promise-retry.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-retry.umd.js","sources":["../src/index.ts"],"sourcesContent":["type
|
|
1
|
+
{"version":3,"file":"promise-retry.umd.js","sources":["../src/index.ts"],"sourcesContent":["type RetryOptions = {\n maxAttempts: number;\n retryDelay: number;\n onError: (err: any, attempt: number) => void;\n};\n\nconst defaultOptions: RetryOptions = {\n maxAttempts: 10,\n retryDelay: 0,\n onError: (err: any, attempt: number) => {},\n};\n\nconst promiseRetry = <T>(\n func: (attempt: number) => Promise<T>,\n options: Partial<RetryOptions> = defaultOptions,\n attempt = 1,\n): Promise<T> => {\n const config: RetryOptions = { ...defaultOptions, ...options };\n\n return func(attempt).catch((err: any) => {\n // For logging...\n config.onError(err, attempt);\n\n if (attempt < config.maxAttempts) {\n return new Promise((resolve) => {\n setTimeout(\n () => resolve(promiseRetry(func, options, attempt + 1)),\n config.retryDelay,\n );\n });\n } else {\n throw err;\n }\n });\n};\n\nconst Retryify = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<RetryOptions> = {},\n) =>\n(...args: Args): Promise<Return> => {\n return promiseRetry<Return>(() => {\n return cb(...args);\n }, options);\n};\n\nexport { promiseRetry, Retryify, type RetryOptions };\n"],"names":["defaultOptions","maxAttempts","retryDelay","onError","err","attempt","promiseRetry","func","options","config","_extends","Promise","resolve","setTimeout","cb","_arguments","arguments","apply","slice","call"],"mappings":"8bAMA,IAAMA,EAA+B,CACnCC,YAAa,GACbC,WAAY,EACZC,QAAS,SAACC,EAAUC,MAGhBC,EAAe,SACnBC,EACAC,EACAH,QADA,IAAAG,IAAAA,EAAiCR,QACjCK,IAAAA,IAAAA,EAAU,GAEV,IAAMI,EAAMC,EAAA,GAAsBV,EAAmBQ,GAErD,OAAOD,EAAKF,GAAQ,MAAO,SAACD,GAI1B,GAFAK,EAAON,QAAQC,EAAKC,GAEhBA,EAAUI,EAAOR,YACnB,OAAW,IAAAU,QAAQ,SAACC,GAClBC,WACE,WAAA,OAAMD,EAAQN,EAAaC,EAAMC,EAASH,EAAU,GAAG,EACvDI,EAAOP,WAEX,GAEA,MAAME,CAEV,EACF,aAEiB,SACfU,EACAN,GAEF,YAFmC,IAAjCA,IAAAA,EAAiC,CAAE,GAErC,eAAmCO,EAAAC,UACjC,OAAOV,EAAqB,WAC1B,OAAOQ,EAAEG,gBAAAC,MAAAC,KAAAJ,GACX,EAAGP,EACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chriscdn/promise-retry",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Retry a function returning a rejected promise until it resolves.",
|
|
5
5
|
"repository": "https://github.com/chriscdn/promise-retry",
|
|
6
6
|
"author": "Christopher Meyer <chris@schwiiz.org>",
|
|
@@ -18,13 +18,18 @@
|
|
|
18
18
|
"types": "./lib/index.d.ts",
|
|
19
19
|
"scripts": {
|
|
20
20
|
"build": "rm -rf ./lib/ && microbundle",
|
|
21
|
-
"dev": "microbundle watch"
|
|
21
|
+
"dev": "microbundle watch",
|
|
22
|
+
"test": "vitest"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
|
-
"microbundle": "^0.15.1"
|
|
25
|
+
"microbundle": "^0.15.1",
|
|
26
|
+
"vitest": "^4.0.6"
|
|
25
27
|
},
|
|
26
28
|
"keywords": [
|
|
27
29
|
"promise",
|
|
28
30
|
"retry"
|
|
31
|
+
],
|
|
32
|
+
"files": [
|
|
33
|
+
"lib"
|
|
29
34
|
]
|
|
30
35
|
}
|
package/src/index.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
type Options = {
|
|
2
|
-
maxAttempts: number;
|
|
3
|
-
retryDelay: number;
|
|
4
|
-
onError: (err: any, attempt: number) => void;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
const defaultOptions: Options = {
|
|
8
|
-
maxAttempts: 10,
|
|
9
|
-
retryDelay: 0,
|
|
10
|
-
onError: (err: any, attempt: number) => {},
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const promiseRetry = <T>(
|
|
14
|
-
func: (attempt: number) => Promise<T>,
|
|
15
|
-
options: Partial<Options> = defaultOptions,
|
|
16
|
-
attempt = 1,
|
|
17
|
-
): Promise<T> => {
|
|
18
|
-
const config = { ...defaultOptions, ...options };
|
|
19
|
-
|
|
20
|
-
return func(attempt).catch((err: any) => {
|
|
21
|
-
// For logging...
|
|
22
|
-
config.onError(err, attempt);
|
|
23
|
-
|
|
24
|
-
if (attempt < config.maxAttempts) {
|
|
25
|
-
return new Promise((resolve) => {
|
|
26
|
-
setTimeout(
|
|
27
|
-
() => resolve(promiseRetry(func, options, attempt + 1)),
|
|
28
|
-
config.retryDelay,
|
|
29
|
-
);
|
|
30
|
-
});
|
|
31
|
-
} else {
|
|
32
|
-
throw err;
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export default promiseRetry;
|
package/src/test.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const promiseRetry = require('./index')
|
|
2
|
-
async function retryMe(name, attempt) {
|
|
3
|
-
console.log(`${name} - ${attempt}`)
|
|
4
|
-
if (coinFlip()) {
|
|
5
|
-
throw new Error('shit')
|
|
6
|
-
} else {
|
|
7
|
-
return `good: ${attempt}`
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function coinFlip() {
|
|
12
|
-
return !Math.floor(Math.random() * 2)
|
|
13
|
-
}
|
|
14
|
-
/*
|
|
15
|
-
promiseRetry((attempt) => retryMe("bob", attempt), {
|
|
16
|
-
maxAttempts: 10,
|
|
17
|
-
milliseconds: 1000,
|
|
18
|
-
})
|
|
19
|
-
.then((poo) => {
|
|
20
|
-
console.log(`poo: ${poo}`);
|
|
21
|
-
})
|
|
22
|
-
.catch((err) => {
|
|
23
|
-
console.log("bummer");
|
|
24
|
-
});
|
|
25
|
-
*/
|
|
26
|
-
;(async () => {
|
|
27
|
-
const value = await promiseRetry(
|
|
28
|
-
async (attempt) => {
|
|
29
|
-
if (coinFlip()) {
|
|
30
|
-
return attempt
|
|
31
|
-
} else {
|
|
32
|
-
console.log('nope')
|
|
33
|
-
throw new Error('nope')
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
milliseconds: 1000,
|
|
38
|
-
}
|
|
39
|
-
)
|
|
40
|
-
console.log(value)
|
|
41
|
-
})()
|