es-toolkit 1.31.0-dev.994 → 1.31.0-dev.995

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.
@@ -36,6 +36,7 @@ export { noop } from '../function/noop.mjs';
36
36
  export { once } from '../function/once.mjs';
37
37
  export { partial } from '../function/partial.mjs';
38
38
  export { partialRight } from '../function/partialRight.mjs';
39
+ export { retry } from '../function/retry.mjs';
39
40
  export { ThrottledFunction } from '../function/throttle.mjs';
40
41
  export { unary } from '../function/unary.mjs';
41
42
  export { mean } from '../math/mean.mjs';
@@ -36,6 +36,7 @@ export { noop } from '../function/noop.js';
36
36
  export { once } from '../function/once.js';
37
37
  export { partial } from '../function/partial.js';
38
38
  export { partialRight } from '../function/partialRight.js';
39
+ export { retry } from '../function/retry.js';
39
40
  export { ThrottledFunction } from '../function/throttle.js';
40
41
  export { unary } from '../function/unary.js';
41
42
  export { mean } from '../math/mean.js';
@@ -3,8 +3,9 @@
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
5
  const zipWith = require('../_chunk/zipWith-Bdyzuy.js');
6
+ const AbortError = require('../_chunk/AbortError-Cg4ZQ1.js');
6
7
  const error_index = require('../error/index.js');
7
- const unary = require('../_chunk/unary-B6qG7C.js');
8
+ const unary = require('../_chunk/unary-c1NFA5.js');
8
9
  const noop = require('../_chunk/noop-2IwLUk.js');
9
10
  const range$1 = require('../_chunk/range-HnEIT7.js');
10
11
  const randomInt = require('../_chunk/randomInt-CF7bZK.js');
@@ -3103,7 +3104,7 @@ exports.xorBy = zipWith.xorBy;
3103
3104
  exports.xorWith = zipWith.xorWith;
3104
3105
  exports.zipObject = zipWith.zipObject;
3105
3106
  exports.zipWith = zipWith.zipWith;
3106
- exports.AbortError = error_index.AbortError;
3107
+ exports.AbortError = AbortError.AbortError;
3107
3108
  exports.TimeoutError = error_index.TimeoutError;
3108
3109
  exports.asyncNoop = unary.asyncNoop;
3109
3110
  exports.identity = unary.identity;
@@ -3111,6 +3112,7 @@ exports.memoize = unary.memoize;
3111
3112
  exports.once = unary.once;
3112
3113
  exports.partial = unary.partial;
3113
3114
  exports.partialRight = unary.partialRight;
3115
+ exports.retry = unary.retry;
3114
3116
  exports.unary = unary.unary;
3115
3117
  exports.noop = noop.noop;
3116
3118
  exports.mean = range$1.mean;
@@ -36,6 +36,7 @@ export { noop } from '../function/noop.mjs';
36
36
  export { once } from '../function/once.mjs';
37
37
  export { partial } from '../function/partial.mjs';
38
38
  export { partialRight } from '../function/partialRight.mjs';
39
+ export { retry } from '../function/retry.mjs';
39
40
  export { unary } from '../function/unary.mjs';
40
41
  export { mean } from '../math/mean.mjs';
41
42
  export { meanBy } from '../math/meanBy.mjs';
@@ -2,12 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- class AbortError extends Error {
6
- constructor(message = 'The operation was aborted') {
7
- super(message);
8
- this.name = 'AbortError';
9
- }
10
- }
5
+ const AbortError = require('../_chunk/AbortError-Cg4ZQ1.js');
11
6
 
12
7
  class TimeoutError extends Error {
13
8
  constructor(message = 'The operation was timed out') {
@@ -16,5 +11,5 @@ class TimeoutError extends Error {
16
11
  }
17
12
  }
18
13
 
19
- exports.AbortError = AbortError;
14
+ exports.AbortError = AbortError.AbortError;
20
15
  exports.TimeoutError = TimeoutError;
@@ -15,6 +15,7 @@ export { once } from './once.mjs';
15
15
  export { partial } from './partial.mjs';
16
16
  export { partialRight } from './partialRight.mjs';
17
17
  export { rest } from './rest.mjs';
18
+ export { retry } from './retry.mjs';
18
19
  export { spread } from './spread.mjs';
19
20
  export { ThrottledFunction, throttle } from './throttle.mjs';
20
21
  export { unary } from './unary.mjs';
@@ -15,6 +15,7 @@ export { once } from './once.js';
15
15
  export { partial } from './partial.js';
16
16
  export { partialRight } from './partialRight.js';
17
17
  export { rest } from './rest.js';
18
+ export { retry } from './retry.js';
18
19
  export { spread } from './spread.js';
19
20
  export { ThrottledFunction, throttle } from './throttle.js';
20
21
  export { unary } from './unary.js';
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const unary = require('../_chunk/unary-B6qG7C.js');
5
+ const unary = require('../_chunk/unary-c1NFA5.js');
6
6
  const noop = require('../_chunk/noop-2IwLUk.js');
7
7
 
8
8
  function before(n, func) {
@@ -98,6 +98,7 @@ exports.once = unary.once;
98
98
  exports.partial = unary.partial;
99
99
  exports.partialRight = unary.partialRight;
100
100
  exports.rest = unary.rest;
101
+ exports.retry = unary.retry;
101
102
  exports.unary = unary.unary;
102
103
  exports.noop = noop.noop;
103
104
  exports.before = before;
@@ -15,6 +15,7 @@ export { once } from './once.mjs';
15
15
  export { partial } from './partial.mjs';
16
16
  export { partialRight } from './partialRight.mjs';
17
17
  export { rest } from './rest.mjs';
18
+ export { retry } from './retry.mjs';
18
19
  export { spread } from './spread.mjs';
19
20
  export { throttle } from './throttle.mjs';
20
21
  export { unary } from './unary.mjs';
@@ -0,0 +1,59 @@
1
+ interface RetryOptions {
2
+ /**
3
+ * The number of milliseconds to interval delay.
4
+ * @default 0
5
+ */
6
+ delay?: number;
7
+ /**
8
+ * The number of retries to attempt.
9
+ * @default Number.POSITIVE_INFINITY
10
+ */
11
+ retries?: number;
12
+ /**
13
+ * An AbortSignal to cancel the retry operation.
14
+ */
15
+ signal?: AbortSignal;
16
+ }
17
+ /**
18
+ * Retries a function that returns a promise until it resolves successfully.
19
+ *
20
+ * @template T
21
+ * @param {() => Promise<T>} func - The function to retry.
22
+ * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
23
+ *
24
+ * @example
25
+ * // Basic usage with default retry options
26
+ * retry(() => fetchData()).then(data => console.log(data));
27
+ */
28
+ declare function retry<T>(func: () => Promise<T>): Promise<T>;
29
+ /**
30
+ * Retries a function that returns a promise a specified number of times.
31
+ *
32
+ * @template T
33
+ * @param {() => Promise<T>} func - The function to retry. It should return a promise.
34
+ * @param {number} retries - The number of retries to attempt. Default is Infinity.
35
+ * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
36
+ *
37
+ * @example
38
+ * // Retry a function up to 3 times
39
+ * retry(() => fetchData(), 3).then(data => console.log(data));
40
+ */
41
+ declare function retry<T>(func: () => Promise<T>, retries: number): Promise<T>;
42
+ /**
43
+ * Retries a function that returns a promise with specified options.
44
+ *
45
+ * @template T
46
+ * @param {() => Promise<T>} func - The function to retry. It should return a promise.
47
+ * @param {RetryOptions} options - Options to configure the retry behavior.
48
+ * @param {number} [options.delay=0] - The number of milliseconds to wait between retries.
49
+ * @param {number} [options.retries=Infinity] - The number of retries to attempt.
50
+ * @param {AbortSignal} [options.signal] - An AbortSignal to cancel the retry operation.
51
+ * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
52
+ *
53
+ * @example
54
+ * // Retry a function with a delay of 1000ms between attempts
55
+ * retry(() => fetchData(), { delay: 1000, times: 5 }).then(data => console.log(data));
56
+ */
57
+ declare function retry<T>(func: () => Promise<T>, options: RetryOptions): Promise<T>;
58
+
59
+ export { retry };
@@ -0,0 +1,59 @@
1
+ interface RetryOptions {
2
+ /**
3
+ * The number of milliseconds to interval delay.
4
+ * @default 0
5
+ */
6
+ delay?: number;
7
+ /**
8
+ * The number of retries to attempt.
9
+ * @default Number.POSITIVE_INFINITY
10
+ */
11
+ retries?: number;
12
+ /**
13
+ * An AbortSignal to cancel the retry operation.
14
+ */
15
+ signal?: AbortSignal;
16
+ }
17
+ /**
18
+ * Retries a function that returns a promise until it resolves successfully.
19
+ *
20
+ * @template T
21
+ * @param {() => Promise<T>} func - The function to retry.
22
+ * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
23
+ *
24
+ * @example
25
+ * // Basic usage with default retry options
26
+ * retry(() => fetchData()).then(data => console.log(data));
27
+ */
28
+ declare function retry<T>(func: () => Promise<T>): Promise<T>;
29
+ /**
30
+ * Retries a function that returns a promise a specified number of times.
31
+ *
32
+ * @template T
33
+ * @param {() => Promise<T>} func - The function to retry. It should return a promise.
34
+ * @param {number} retries - The number of retries to attempt. Default is Infinity.
35
+ * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
36
+ *
37
+ * @example
38
+ * // Retry a function up to 3 times
39
+ * retry(() => fetchData(), 3).then(data => console.log(data));
40
+ */
41
+ declare function retry<T>(func: () => Promise<T>, retries: number): Promise<T>;
42
+ /**
43
+ * Retries a function that returns a promise with specified options.
44
+ *
45
+ * @template T
46
+ * @param {() => Promise<T>} func - The function to retry. It should return a promise.
47
+ * @param {RetryOptions} options - Options to configure the retry behavior.
48
+ * @param {number} [options.delay=0] - The number of milliseconds to wait between retries.
49
+ * @param {number} [options.retries=Infinity] - The number of retries to attempt.
50
+ * @param {AbortSignal} [options.signal] - An AbortSignal to cancel the retry operation.
51
+ * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
52
+ *
53
+ * @example
54
+ * // Retry a function with a delay of 1000ms between attempts
55
+ * retry(() => fetchData(), { delay: 1000, times: 5 }).then(data => console.log(data));
56
+ */
57
+ declare function retry<T>(func: () => Promise<T>, options: RetryOptions): Promise<T>;
58
+
59
+ export { retry };
@@ -0,0 +1,35 @@
1
+ import { delay } from '../promise/delay.mjs';
2
+
3
+ const DEFAULT_DELAY = 0;
4
+ const DEFAULT_RETRIES = Number.POSITIVE_INFINITY;
5
+ async function retry(func, _options) {
6
+ let delay$1;
7
+ let retries;
8
+ let signal;
9
+ if (typeof _options === 'number') {
10
+ delay$1 = DEFAULT_DELAY;
11
+ retries = _options;
12
+ signal = undefined;
13
+ }
14
+ else {
15
+ delay$1 = _options?.delay ?? DEFAULT_DELAY;
16
+ retries = _options?.retries ?? DEFAULT_RETRIES;
17
+ signal = _options?.signal;
18
+ }
19
+ let error;
20
+ for (let i = 0; i < retries; i++) {
21
+ if (signal?.aborted) {
22
+ throw error ?? new Error(`The retry operation was aborted due to an abort signal.`);
23
+ }
24
+ try {
25
+ return await func();
26
+ }
27
+ catch (err) {
28
+ error = err;
29
+ await delay(delay$1);
30
+ }
31
+ }
32
+ throw error;
33
+ }
34
+
35
+ export { retry };
package/dist/index.d.mts CHANGED
@@ -77,6 +77,7 @@ export { once } from './function/once.mjs';
77
77
  export { partial } from './function/partial.mjs';
78
78
  export { partialRight } from './function/partialRight.mjs';
79
79
  export { rest } from './function/rest.mjs';
80
+ export { retry } from './function/retry.mjs';
80
81
  export { spread } from './function/spread.mjs';
81
82
  export { ThrottledFunction, throttle } from './function/throttle.mjs';
82
83
  export { unary } from './function/unary.mjs';
package/dist/index.d.ts CHANGED
@@ -77,6 +77,7 @@ export { once } from './function/once.js';
77
77
  export { partial } from './function/partial.js';
78
78
  export { partialRight } from './function/partialRight.js';
79
79
  export { rest } from './function/rest.js';
80
+ export { retry } from './function/retry.js';
80
81
  export { spread } from './function/spread.js';
81
82
  export { ThrottledFunction, throttle } from './function/throttle.js';
82
83
  export { unary } from './function/unary.js';
package/dist/index.js CHANGED
@@ -4,8 +4,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
5
  const zipWith = require('./_chunk/zipWith-Bdyzuy.js');
6
6
  const array_index = require('./array/index.js');
7
+ const AbortError = require('./_chunk/AbortError-Cg4ZQ1.js');
7
8
  const error_index = require('./error/index.js');
8
- const unary = require('./_chunk/unary-B6qG7C.js');
9
+ const unary = require('./_chunk/unary-c1NFA5.js');
9
10
  const function_index = require('./function/index.js');
10
11
  const noop = require('./_chunk/noop-2IwLUk.js');
11
12
  const range = require('./_chunk/range-HnEIT7.js');
@@ -16,6 +17,7 @@ const object_index = require('./object/index.js');
16
17
  const isWeakSet = require('./_chunk/isWeakSet-CvIdTA.js');
17
18
  const predicate_index = require('./predicate/index.js');
18
19
  const isPlainObject = require('./_chunk/isPlainObject-octpoD.js');
20
+ const delay = require('./_chunk/delay-_VMfFa.js');
19
21
  const promise_index = require('./promise/index.js');
20
22
  const upperFirst = require('./_chunk/upperFirst-CorAVn.js');
21
23
  const string_index = require('./string/index.js');
@@ -83,7 +85,7 @@ exports.zipWith = zipWith.zipWith;
83
85
  exports.orderBy = array_index.orderBy;
84
86
  exports.sortBy = array_index.sortBy;
85
87
  exports.takeRightWhile = array_index.takeRightWhile;
86
- exports.AbortError = error_index.AbortError;
88
+ exports.AbortError = AbortError.AbortError;
87
89
  exports.TimeoutError = error_index.TimeoutError;
88
90
  exports.after = unary.after;
89
91
  exports.ary = unary.ary;
@@ -98,6 +100,7 @@ exports.once = unary.once;
98
100
  exports.partial = unary.partial;
99
101
  exports.partialRight = unary.partialRight;
100
102
  exports.rest = unary.rest;
103
+ exports.retry = unary.retry;
101
104
  exports.unary = unary.unary;
102
105
  exports.before = function_index.before;
103
106
  exports.curry = function_index.curry;
@@ -161,7 +164,7 @@ exports.isSymbol = predicate_index.isSymbol;
161
164
  exports.isPlainObject = isPlainObject.isPlainObject;
162
165
  exports.isPrimitive = isPlainObject.isPrimitive;
163
166
  exports.isTypedArray = isPlainObject.isTypedArray;
164
- exports.delay = promise_index.delay;
167
+ exports.delay = delay.delay;
165
168
  exports.timeout = promise_index.timeout;
166
169
  exports.withTimeout = promise_index.withTimeout;
167
170
  exports.camelCase = upperFirst.camelCase;
package/dist/index.mjs CHANGED
@@ -77,6 +77,7 @@ export { once } from './function/once.mjs';
77
77
  export { partial } from './function/partial.mjs';
78
78
  export { partialRight } from './function/partialRight.mjs';
79
79
  export { rest } from './function/rest.mjs';
80
+ export { retry } from './function/retry.mjs';
80
81
  export { spread } from './function/spread.mjs';
81
82
  export { throttle } from './function/throttle.mjs';
82
83
  export { unary } from './function/unary.mjs';
@@ -2,30 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
+ const delay = require('../_chunk/delay-_VMfFa.js');
5
6
  const error_index = require('../error/index.js');
6
7
 
7
- function delay(ms, { signal } = {}) {
8
- return new Promise((resolve, reject) => {
9
- const abortError = () => {
10
- reject(new error_index.AbortError());
11
- };
12
- const abortHandler = () => {
13
- clearTimeout(timeoutId);
14
- abortError();
15
- };
16
- if (signal?.aborted) {
17
- return abortError();
18
- }
19
- const timeoutId = setTimeout(() => {
20
- signal?.removeEventListener('abort', abortHandler);
21
- resolve();
22
- }, ms);
23
- signal?.addEventListener('abort', abortHandler, { once: true });
24
- });
25
- }
26
-
27
8
  async function timeout(ms) {
28
- await delay(ms);
9
+ await delay.delay(ms);
29
10
  throw new error_index.TimeoutError();
30
11
  }
31
12
 
@@ -33,6 +14,6 @@ async function withTimeout(run, ms) {
33
14
  return Promise.race([run(), timeout(ms)]);
34
15
  }
35
16
 
36
- exports.delay = delay;
17
+ exports.delay = delay.delay;
37
18
  exports.timeout = timeout;
38
19
  exports.withTimeout = withTimeout;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.31.0-dev.994+14843eed",
4
+ "version": "1.31.0-dev.995+06bdc0fc",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {