@chriscdn/promise-retry 3.0.1 → 4.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 +4 -4
- package/lib/index.d.ts +49 -8
- package/lib/index.js +33 -0
- package/lib/index.js.map +1 -0
- package/package.json +9 -14
- package/lib/promise-retry.cjs +0 -2
- package/lib/promise-retry.cjs.map +0 -1
- package/lib/promise-retry.modern.js +0 -2
- package/lib/promise-retry.modern.js.map +0 -1
- package/lib/promise-retry.module.js +0 -2
- package/lib/promise-retry.module.js.map +0 -1
- package/lib/promise-retry.umd.js +0 -2
- package/lib/promise-retry.umd.js.map +0 -1
package/README.md
CHANGED
|
@@ -49,12 +49,12 @@ const results = await promiseRetry(async (attempt) => {
|
|
|
49
49
|
}, options);
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
## Example 2 -
|
|
52
|
+
## Example 2 - retryify
|
|
53
53
|
|
|
54
|
-
`
|
|
54
|
+
The `retryify` function 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
59
|
const myAsyncFunctionRandomFails = async (a, b) => {
|
|
60
60
|
if (Math.random() < 0.2) {
|
|
@@ -64,7 +64,7 @@ const myAsyncFunctionRandomFails = async (a, b) => {
|
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
-
const myAsyncFunctionRetry =
|
|
67
|
+
const myAsyncFunctionRetry = retryify(myAsyncFunctionRandomFails, options);
|
|
68
68
|
|
|
69
69
|
try {
|
|
70
70
|
const sum = await myAsyncFunctionRetry(1, 5);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for retry behavior.
|
|
3
|
+
*/
|
|
4
|
+
type RetryOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Maximum number of attempts before giving up. Includes the initial attempt.
|
|
7
|
+
*/
|
|
8
|
+
maxAttempts: number;
|
|
9
|
+
/**
|
|
10
|
+
* Delay in milliseconds between retry attempts.
|
|
11
|
+
*/
|
|
12
|
+
retryDelay: number;
|
|
13
|
+
/**
|
|
14
|
+
* Callback invoked after a failed attempt. Receives the thrown error and the
|
|
15
|
+
* current attempt number.
|
|
16
|
+
*/
|
|
17
|
+
onError: (err: any, attempt: number) => void;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Retries an asynchronous function until it resolves or the maximum number of
|
|
21
|
+
* attempts is reached.
|
|
22
|
+
*
|
|
23
|
+
* The provided function is invoked immediately. If it rejects, it will be
|
|
24
|
+
* retried according to the configured options.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam T - The resolved value type of the promise.
|
|
27
|
+
* @param func - A function returning a promise. Receives the current attempt
|
|
28
|
+
* number.
|
|
29
|
+
* @param options - Optional retry configuration that overrides defaults.
|
|
30
|
+
* @param attempt - Internal attempt counter. Do not set manually.
|
|
31
|
+
* @returns A promise that resolves with the successful result or rejects with
|
|
32
|
+
* the last encountered error after exhausting retries.
|
|
33
|
+
*/
|
|
34
|
+
declare const promiseRetry: <T>(func: (attempt: number) => Promise<T>, options?: Partial<RetryOptions>, attempt?: number) => Promise<T>;
|
|
35
|
+
/**
|
|
36
|
+
* Wraps an asynchronous function with retry behavior.
|
|
37
|
+
*
|
|
38
|
+
* The returned function preserves the original arguments and automatically
|
|
39
|
+
* retries the underlying promise if it rejects.
|
|
40
|
+
*
|
|
41
|
+
* @typeParam Args - Argument types of the wrapped function.
|
|
42
|
+
* @typeParam Return - Resolved value type of the wrapped promise.
|
|
43
|
+
* @param cb - The asynchronous function to wrap.
|
|
44
|
+
* @param options - Optional retry configuration.
|
|
45
|
+
* @returns A new function with identical parameters that applies retry logic.
|
|
46
|
+
*/
|
|
47
|
+
declare const retryify: <Args extends unknown[], Return>(cb: (...args: Args) => Promise<Return>, options?: Partial<RetryOptions>) => (...args: Args) => Promise<Return>;
|
|
48
|
+
|
|
49
|
+
export { type RetryOptions, promiseRetry, retryify };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var defaultOptions = {
|
|
3
|
+
maxAttempts: 10,
|
|
4
|
+
retryDelay: 0,
|
|
5
|
+
onError: (_err, _attempt) => {
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
var promiseRetry = (func, options = defaultOptions, attempt = 1) => {
|
|
9
|
+
const config = { ...defaultOptions, ...options };
|
|
10
|
+
return func(attempt).catch((err) => {
|
|
11
|
+
config.onError(err, attempt);
|
|
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
|
+
};
|
|
24
|
+
var retryify = (cb, options = {}) => (...args) => {
|
|
25
|
+
return promiseRetry(() => {
|
|
26
|
+
return cb(...args);
|
|
27
|
+
}, options);
|
|
28
|
+
};
|
|
29
|
+
export {
|
|
30
|
+
promiseRetry,
|
|
31
|
+
retryify
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Configuration options for retry behavior.\n */\ntype RetryOptions = {\n /**\n * Maximum number of attempts before giving up. Includes the initial attempt.\n */\n maxAttempts: number;\n\n /**\n * Delay in milliseconds between retry attempts.\n */\n retryDelay: number;\n\n /**\n * Callback invoked after a failed attempt. Receives the thrown error and the\n * current attempt number.\n */\n onError: (err: any, attempt: number) => void;\n};\n\n/**\n * Default retry configuration.\n */\nconst defaultOptions: RetryOptions = {\n maxAttempts: 10,\n retryDelay: 0,\n onError: (_err: unknown, _attempt: number) => {},\n};\n\n/**\n * Retries an asynchronous function until it resolves or the maximum number of\n * attempts is reached.\n *\n * The provided function is invoked immediately. If it rejects, it will be\n * retried according to the configured options.\n *\n * @typeParam T - The resolved value type of the promise.\n * @param func - A function returning a promise. Receives the current attempt\n * number.\n * @param options - Optional retry configuration that overrides defaults.\n * @param attempt - Internal attempt counter. Do not set manually.\n * @returns A promise that resolves with the successful result or rejects with\n * the last encountered error after exhausting retries.\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: unknown) => {\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\n/**\n * Wraps an asynchronous function with retry behavior.\n *\n * The returned function preserves the original arguments and automatically\n * retries the underlying promise if it rejects.\n *\n * @typeParam Args - Argument types of the wrapped function.\n * @typeParam Return - Resolved value type of the wrapped promise.\n * @param cb - The asynchronous function to wrap.\n * @param options - Optional retry configuration.\n * @returns A new function with identical parameters that applies retry logic.\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"],"mappings":";AAwBA,IAAM,iBAA+B;AAAA,EACnC,aAAa;AAAA,EACb,YAAY;AAAA,EACZ,SAAS,CAAC,MAAe,aAAqB;AAAA,EAAC;AACjD;AAiBA,IAAM,eAAe,CACnB,MACA,UAAiC,gBACjC,UAAU,MACK;AACf,QAAM,SAAuB,EAAE,GAAG,gBAAgB,GAAG,QAAQ;AAE7D,SAAO,KAAK,OAAO,EAAE,MAAM,CAAC,QAAiB;AAC3C,WAAO,QAAQ,KAAK,OAAO;AAE3B,QAAI,UAAU,OAAO,aAAa;AAChC,aAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B;AAAA,UACE,MAAM,QAAQ,aAAa,MAAM,SAAS,UAAU,CAAC,CAAC;AAAA,UACtD,OAAO;AAAA,QACT;AAAA,MACF,CAAC;AAAA,IACH,OAAO;AACL,YAAM;AAAA,IACR;AAAA,EACF,CAAC;AACH;AAcA,IAAM,WAAW,CACf,IACA,UAAiC,CAAC,MAEpC,IAAI,SAAgC;AAClC,SAAO,aAAqB,MAAM;AAChC,WAAO,GAAG,GAAG,IAAI;AAAA,EACnB,GAAG,OAAO;AACZ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chriscdn/promise-retry",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
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>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"type": "module",
|
|
9
|
-
"
|
|
10
|
-
"main": "./lib/promise-retry.cjs",
|
|
11
|
-
"module": "./lib/promise-retry.module.js",
|
|
12
|
-
"unpkg": "./lib/promise-retry.umd.js",
|
|
13
|
-
"exports": {
|
|
14
|
-
"types": "./lib/index.d.ts",
|
|
15
|
-
"require": "./lib/promise-retry.cjs",
|
|
16
|
-
"default": "./lib/promise-retry.modern.js"
|
|
17
|
-
},
|
|
9
|
+
"main": "./lib/index.js",
|
|
18
10
|
"types": "./lib/index.d.ts",
|
|
11
|
+
"exports": "./lib/index.js",
|
|
19
12
|
"scripts": {
|
|
20
|
-
"build": "
|
|
21
|
-
"
|
|
13
|
+
"build": "tsup",
|
|
14
|
+
"watch": "yarn build --watch",
|
|
22
15
|
"test": "vitest"
|
|
23
16
|
},
|
|
24
17
|
"devDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
18
|
+
"@tsconfig/strictest": "^2.0.8",
|
|
19
|
+
"tsup": "^8.5.1",
|
|
20
|
+
"typescript": "^5.9.3",
|
|
21
|
+
"vitest": "^4.0.8"
|
|
27
22
|
},
|
|
28
23
|
"keywords": [
|
|
29
24
|
"promise",
|
package/lib/promise-retry.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=promise-retry.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=promise-retry.modern.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=promise-retry.module.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=promise-retry.umd.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|