@superutils/promise 1.0.3 → 1.0.5
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/dist/index.d.ts +122 -69
- package/dist/index.js +79 -67
- package/package.json +5 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _superutils_core from '@superutils/core';
|
|
2
|
-
import { ValueOrPromise, TimeoutId,
|
|
2
|
+
import { ValueOrPromise, TimeoutId, PositiveNumber, ThrottleOptions, DeferredOptions } from '@superutils/core';
|
|
3
3
|
|
|
4
4
|
interface IPromisE<T = unknown> extends Promise<T> {
|
|
5
5
|
/** 0: pending, 1: resolved, 2: rejected */
|
|
@@ -77,25 +77,32 @@ type OnEarlyFinalize<T> = <TResolved extends boolean, TValue = TResolved extends
|
|
|
77
77
|
type PromiseParams<T = unknown> = ConstructorParameters<typeof Promise<T>>;
|
|
78
78
|
|
|
79
79
|
/** Return type of `PromisE.deferred()` */
|
|
80
|
-
type
|
|
81
|
-
type
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
type DeferredAsyncCallback<TArgs extends unknown[] | [] = []> = <TResult = unknown>(promise: Promise<TResult> | ((...args: TArgs) => Promise<TResult>)) => IPromisE<TResult>;
|
|
81
|
+
type DeferredAsyncGetPromise<T> = <TResult = T>() => Promise<TResult>;
|
|
82
|
+
/** Default options used by `PromisE.deferred` and related functions */
|
|
83
|
+
type DeferredAsyncDefaults = Pick<Required<DeferredAsyncOptions>, 'delayMs' | 'resolveError' | 'resolveIgnored'>;
|
|
84
|
+
/** Options for `PromisE.deferred` and other related functions */
|
|
85
|
+
type DeferredAsyncOptions<ThisArg = unknown, DelayMs extends number = number> = {
|
|
86
|
+
/**
|
|
87
|
+
* Delay in milliseconds, used for `debounce` and `throttle` modes.
|
|
88
|
+
*
|
|
89
|
+
* Default: `100`
|
|
90
|
+
*/
|
|
84
91
|
/** Callback invoked whenever promise/function throws error */
|
|
85
|
-
onError?: (err: unknown) => ValueOrPromise<unknown>;
|
|
92
|
+
onError?: (this: ThisArg, err: unknown) => ValueOrPromise<unknown>;
|
|
86
93
|
/**
|
|
87
94
|
* Whenever a promise/function is ignored when in debource/throttle mode, `onIgnored` wil be invoked.
|
|
88
95
|
* The promise/function will not be invoked, unless it's manually invoked using the `ignored` function.
|
|
89
96
|
* Use for debugging or logging purposes.
|
|
90
97
|
*/
|
|
91
|
-
onIgnore?: (
|
|
98
|
+
onIgnore?: (this: ThisArg, ignored: DeferredAsyncGetPromise<unknown>) => ValueOrPromise<unknown>;
|
|
92
99
|
/**
|
|
93
100
|
* Whenever a promise/function is executed successfully `onResult` will be called.
|
|
94
101
|
* Those that are ignored but resolve with last will not cause `onResult` to be invoked.
|
|
95
102
|
*
|
|
96
103
|
* Result can be `undefined` if `ResolveIgnored.WITH_UNDEFINED` is used.
|
|
97
104
|
*/
|
|
98
|
-
onResult?: (result?:
|
|
105
|
+
onResult?: (this: ThisArg, result?: any) => ValueOrPromise<unknown>;
|
|
99
106
|
/**
|
|
100
107
|
* Indicates what to do when a promise in the queue is ignored.
|
|
101
108
|
* See {@link ResolveIgnored} for available options.
|
|
@@ -106,16 +113,18 @@ type DeferredOptions<ThisArg = unknown> = {
|
|
|
106
113
|
* See {@link ResolveError} for available options.
|
|
107
114
|
*/
|
|
108
115
|
resolveError?: ResolveError;
|
|
109
|
-
/**
|
|
110
|
-
|
|
116
|
+
/** The value to be used as "thisArg" whenever any of the callbacks are invoked */
|
|
117
|
+
thisArg?: ThisArg;
|
|
111
118
|
} & (({
|
|
112
|
-
|
|
119
|
+
/** Throttle duration in milliseconds */
|
|
120
|
+
delayMs?: PositiveNumber<DelayMs>;
|
|
113
121
|
throttle: true;
|
|
114
|
-
} &
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
122
|
+
} & Omit<ThrottleOptions, 'onError' | 'ThisArg' | 'tid'>) | ({
|
|
123
|
+
/** Debounce/deferred duration in milliseconds */
|
|
124
|
+
delayMs?: PositiveNumber<DelayMs>;
|
|
125
|
+
throttle?: false | undefined;
|
|
126
|
+
} & Omit<DeferredOptions, 'onError' | 'ThisArg' | 'tid'>));
|
|
127
|
+
/** Determines what to do when deferred promise/function fails */
|
|
119
128
|
declare enum ResolveError {
|
|
120
129
|
/** Neither resolve nor reject the failed */
|
|
121
130
|
NEVER = "NEVER",
|
|
@@ -126,7 +135,10 @@ declare enum ResolveError {
|
|
|
126
135
|
/** Resolve with undefined */
|
|
127
136
|
WITH_UNDEFINED = "RESOLVE_UNDEFINED"
|
|
128
137
|
}
|
|
129
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Determines what to do when a promise/callback is ignored, either because of being
|
|
140
|
+
* deferred, throttled or another been prioritized.
|
|
141
|
+
*/
|
|
130
142
|
declare enum ResolveIgnored {
|
|
131
143
|
/** Never resolve ignored promises. Caution: make sure this doesn't cause any memory leaks. */
|
|
132
144
|
NEVER = "NEVER",
|
|
@@ -136,22 +148,6 @@ declare enum ResolveIgnored {
|
|
|
136
148
|
WITH_UNDEFINED = "WITH_UNDEFINED"
|
|
137
149
|
}
|
|
138
150
|
|
|
139
|
-
/** Global configuration */
|
|
140
|
-
declare const config: {
|
|
141
|
-
/** Default value for `options` used by `PromisE.*deferred*` functions */
|
|
142
|
-
deferOptions: DeferredOptions;
|
|
143
|
-
delayTimeoutMsg: string;
|
|
144
|
-
retryOptions: {
|
|
145
|
-
retry: number;
|
|
146
|
-
retryBackOff: "exponential";
|
|
147
|
-
retryDelay: number;
|
|
148
|
-
retryDelayJitter: true;
|
|
149
|
-
retryDelayJitterMax: number;
|
|
150
|
-
retryIf: null;
|
|
151
|
-
};
|
|
152
|
-
};
|
|
153
|
-
type Config = typeof config;
|
|
154
|
-
|
|
155
151
|
/** Options for automatic retry mechanism */
|
|
156
152
|
type RetryOptions<T = unknown> = {
|
|
157
153
|
/**
|
|
@@ -188,7 +184,7 @@ type RetryOptions<T = unknown> = {
|
|
|
188
184
|
* Additional condition/function to be used to determine whether function should be retried.
|
|
189
185
|
* `retryIf` will only be executed when function execution is successful.
|
|
190
186
|
*/
|
|
191
|
-
retryIf?: null | ((prevResult: T | undefined, retryCount: number, error?: unknown) => boolean);
|
|
187
|
+
retryIf?: null | ((prevResult: T | undefined, retryCount: number, error?: unknown) => boolean | Promise<boolean>);
|
|
192
188
|
};
|
|
193
189
|
|
|
194
190
|
declare class PromisEBase<T = unknown> extends Promise<T> implements IPromisE<T> {
|
|
@@ -360,7 +356,14 @@ type TimeoutOptions<Func extends string = 'all'> = {
|
|
|
360
356
|
* // `5000` will be printed in the console
|
|
361
357
|
* ```
|
|
362
358
|
*/
|
|
363
|
-
declare function deferred<T>(options?:
|
|
359
|
+
declare function deferred<T, ThisArg = unknown, Delay extends number = number>(options?: DeferredAsyncOptions<ThisArg, Delay>): DeferredAsyncCallback;
|
|
360
|
+
declare namespace deferred {
|
|
361
|
+
var defaults: {
|
|
362
|
+
delayMs: number;
|
|
363
|
+
resolveError: ResolveError.REJECT;
|
|
364
|
+
resolveIgnored: ResolveIgnored.WITH_LAST;
|
|
365
|
+
};
|
|
366
|
+
}
|
|
364
367
|
|
|
365
368
|
/**
|
|
366
369
|
* @function PromisE.deferredCallback
|
|
@@ -394,7 +397,7 @@ declare function deferred<T>(options?: DeferredOptions): DeferredReturn;
|
|
|
394
397
|
* // 200, 600, 1100
|
|
395
398
|
* ```
|
|
396
399
|
*/
|
|
397
|
-
declare function deferredCallback<TDefault, CbArgs extends unknown[] = unknown[]>(callback: (...args: CbArgs) => TDefault | Promise<TDefault>, options?:
|
|
400
|
+
declare function deferredCallback<TDefault, ThisArg, Delay extends number = number, CbArgs extends unknown[] = unknown[]>(callback: (...args: CbArgs) => TDefault | Promise<TDefault>, options?: DeferredAsyncOptions<ThisArg, Delay>): <TResult = TDefault>(...args: CbArgs) => IPromisE<TResult>;
|
|
398
401
|
|
|
399
402
|
/**
|
|
400
403
|
* @function PromisE.delay
|
|
@@ -426,6 +429,14 @@ declare function deferredCallback<TDefault, CbArgs extends unknown[] = unknown[]
|
|
|
426
429
|
* ```
|
|
427
430
|
*/
|
|
428
431
|
declare function delay<T = number, TReject extends boolean = boolean>(duration?: number, result?: T | (() => T), asRejected?: TReject): IPromisE_Delay<T>;
|
|
432
|
+
declare namespace delay {
|
|
433
|
+
var defaults: {
|
|
434
|
+
/** Default delay duration in milliseconds */
|
|
435
|
+
duration: number;
|
|
436
|
+
/** Default timed out message */
|
|
437
|
+
delayTimeoutMsg: string;
|
|
438
|
+
};
|
|
439
|
+
}
|
|
429
440
|
|
|
430
441
|
/**
|
|
431
442
|
* @function PromisE.delayReject
|
|
@@ -462,14 +473,16 @@ declare function delay<T = number, TReject extends boolean = boolean>(duration?:
|
|
|
462
473
|
declare function delayReject<T = never>(duration: number, reason?: unknown): IPromisE_Delay<T>;
|
|
463
474
|
|
|
464
475
|
/**
|
|
465
|
-
*
|
|
466
|
-
*
|
|
476
|
+
* Creates a new promise that wraps one or more promises and rejects if they do not settle within a
|
|
477
|
+
* specified timeout duration. When multiple promises are provided, they can be processed using methods like
|
|
478
|
+
* `all` (default), `race`, `any`, or `allSettled`.
|
|
479
|
+
*
|
|
480
|
+
* @param timeout (optional) timeout duration in milliseconds.
|
|
481
|
+
* Default: `10000` (10 seconds)
|
|
467
482
|
*
|
|
468
|
-
* @param
|
|
469
|
-
* Default: `10000` (10 seconds)
|
|
470
|
-
* @param values promise/function: one or more promises as individual arguments
|
|
483
|
+
* @param values rest param containing one or more promises/values
|
|
471
484
|
*
|
|
472
|
-
* @example
|
|
485
|
+
* @example Wokring with a single promise: resolved succesfully
|
|
473
486
|
* ```typescript
|
|
474
487
|
* PromisE.timeout(
|
|
475
488
|
* 5000, // timeout after 5000ms
|
|
@@ -478,7 +491,16 @@ declare function delayReject<T = never>(duration: number, reason?: unknown): IPr
|
|
|
478
491
|
* // Result: 1000
|
|
479
492
|
* ```
|
|
480
493
|
*
|
|
481
|
-
* @example
|
|
494
|
+
* @example Promise times out & rejected
|
|
495
|
+
* ```typescript
|
|
496
|
+
* PromisE.timeout(
|
|
497
|
+
* 5000, // timeout after 5000ms
|
|
498
|
+
* PromisE.delay(20000), // resolves after 20000ms with value 20000
|
|
499
|
+
* ).catch(console.error)
|
|
500
|
+
* // Error: Error('Timed out after 5000ms')
|
|
501
|
+
*```
|
|
502
|
+
*
|
|
503
|
+
* @example Working with multiple promises, resolved using "PromisE.all()"
|
|
482
504
|
*
|
|
483
505
|
* ```typescript
|
|
484
506
|
* PromisE.timeout(
|
|
@@ -490,17 +512,8 @@ declare function delayReject<T = never>(duration: number, reason?: unknown): IPr
|
|
|
490
512
|
* // Result: [ 1000, 2000, 3000 ]
|
|
491
513
|
* ```
|
|
492
514
|
*
|
|
493
|
-
* @example
|
|
494
|
-
*
|
|
495
|
-
* PromisE.timeout(
|
|
496
|
-
* 5000, // timeout after 5000ms
|
|
497
|
-
* PromisE.delay(20000), // resolves after 20000ms with value 20000
|
|
498
|
-
* ).catch(console.error)
|
|
499
|
-
* // Error: Error('Timed out after 5000ms')
|
|
500
|
-
*```
|
|
501
|
-
*
|
|
502
|
-
* @example Example 4: timed out & but not rejected.
|
|
503
|
-
* // Eg: when API request is taking longer than expected, print a message but not reject the promise.
|
|
515
|
+
* @example Promise times out & but not rejected.
|
|
516
|
+
* Eg: when API request is taking longer than expected, print a message avoid rejecting the promise.
|
|
504
517
|
* ```typescript
|
|
505
518
|
* const promise = PromisE.timeout(
|
|
506
519
|
* 5000, // timeout after 5000ms
|
|
@@ -512,13 +525,48 @@ declare function delayReject<T = never>(duration: number, reason?: unknown): IPr
|
|
|
512
525
|
*
|
|
513
526
|
* // promise timed out >> print/update UI
|
|
514
527
|
* console.log('Request is taking longer than expected......')
|
|
515
|
-
* //
|
|
528
|
+
* // Now return the data promise which is the result of `PromisE.all(promises)` (default).
|
|
516
529
|
* return promise.data
|
|
517
530
|
* })
|
|
518
531
|
*```
|
|
532
|
+
*
|
|
533
|
+
* @example Multiple promises resolved using "PromisE.race()"
|
|
534
|
+
*
|
|
535
|
+
* ```typescript
|
|
536
|
+
* PromisE.timeout(
|
|
537
|
+
* { // instead of `timeout: number` an object can be used for additional options
|
|
538
|
+
* func: 'race', // tells PromisE.timeout to use `PromisE.race(promises)`
|
|
539
|
+
* timeout: 5000, // timeout after 5000ms
|
|
540
|
+
* timeoutMsg: 'My custom timed out message',
|
|
541
|
+
* },
|
|
542
|
+
* PromisE.delay(1000), // resolves after 1000ms with value 1000
|
|
543
|
+
* PromisE.delay(2000), // resolves after 2000ms with value 2000
|
|
544
|
+
* PromisE.delay(3000), // resolves after 3000ms with value 3000
|
|
545
|
+
* ).then(console.log)
|
|
546
|
+
* // Result: 1000 (Result of `Promise.race(promises)`)
|
|
547
|
+
* ```
|
|
548
|
+
*/
|
|
549
|
+
declare function timeout<T extends [unknown, ...unknown[]], // require at least one value
|
|
550
|
+
Result = T['length'] extends 1 ? Awaited<T[0]> : Awaited<T[number]>[]>(timeout: number, ...values: T): IPromisE_Timeout<Result>;
|
|
551
|
+
/**
|
|
552
|
+
*
|
|
553
|
+
* @param options An options object can be passed with one or more of the following properties:
|
|
554
|
+
* @param options.func (optional) Name of the `PromisE` method to be used to combine the `values`.
|
|
555
|
+
* Only used when more than one promise is provided.
|
|
556
|
+
*
|
|
557
|
+
* Accepted values:
|
|
558
|
+
* 1. `'all'` **(default)**: for `PromisE.all`
|
|
559
|
+
* 2. `'allSettled'`: for `PromisE.allSettled`
|
|
560
|
+
* 3. `'any'`: for `PromisE.any`
|
|
561
|
+
* 4. `'race'`: for `PromisE.race`
|
|
562
|
+
*
|
|
563
|
+
* @param options.timeout (optional) timeout duration in milliseconds. Default: `10_000` (10 seconds)
|
|
564
|
+
* @param options.timeoutMsg (optional) custom error message to be used when promises timeout.
|
|
565
|
+
*
|
|
566
|
+
* @param values
|
|
519
567
|
*/
|
|
520
568
|
declare function timeout<T extends [unknown, ...unknown[]], // require at least one value
|
|
521
|
-
TFunc extends keyof TimeoutFunc<T>, Result = T['length'] extends 1 ? Awaited<T[0]> : Awaited<ReturnType<TimeoutFunc<T>[TFunc]>>>(
|
|
569
|
+
TFunc extends keyof TimeoutFunc<T>, Result = T['length'] extends 1 ? Awaited<T[0]> : Awaited<ReturnType<TimeoutFunc<T>[TFunc]>>>(options: TimeoutOptions<TFunc>, ...values: T): IPromisE_Timeout<Result>;
|
|
522
570
|
declare namespace timeout {
|
|
523
571
|
var defaultOptions: Required<TimeoutOptions>;
|
|
524
572
|
}
|
|
@@ -564,24 +612,20 @@ declare namespace timeout {
|
|
|
564
612
|
* ```
|
|
565
613
|
*/
|
|
566
614
|
declare class PromisE<T = unknown> extends PromisEBase<T> {
|
|
567
|
-
|
|
568
|
-
static
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
615
|
+
static deferred: typeof deferred;
|
|
616
|
+
static deferredCallback: typeof deferredCallback;
|
|
617
|
+
static delay: typeof delay;
|
|
618
|
+
static delayReject: typeof delayReject;
|
|
619
|
+
static retry: {
|
|
620
|
+
<T_1>(func: () => _superutils_core.ValueOrPromise<T_1>, options: RetryOptions<T_1>): Promise<T_1>;
|
|
621
|
+
defaults: {
|
|
572
622
|
retry: number;
|
|
573
623
|
retryBackOff: "exponential";
|
|
574
624
|
retryDelay: number;
|
|
575
625
|
retryDelayJitter: true;
|
|
576
626
|
retryDelayJitterMax: number;
|
|
577
|
-
retryIf: null;
|
|
578
627
|
};
|
|
579
628
|
};
|
|
580
|
-
static deferred: typeof deferred;
|
|
581
|
-
static deferredCallback: typeof deferredCallback;
|
|
582
|
-
static delay: typeof delay;
|
|
583
|
-
static delayReject: typeof delayReject;
|
|
584
|
-
static retry: <T_1>(func: () => _superutils_core.ValueOrPromise<T_1>, options?: RetryOptions<T_1>) => Promise<T_1>;
|
|
585
629
|
static timeout: typeof timeout;
|
|
586
630
|
}
|
|
587
631
|
|
|
@@ -609,6 +653,15 @@ declare class PromisE<T = unknown> extends PromisEBase<T> {
|
|
|
609
653
|
* If all retries fail (either by throwing an error or by the condition function always returning true),
|
|
610
654
|
* it resolves with `undefined`. Errors thrown by `func` are caught and handled internally, not re-thrown.
|
|
611
655
|
*/
|
|
612
|
-
declare const retry:
|
|
656
|
+
declare const retry: {
|
|
657
|
+
<T>(func: () => ValueOrPromise<T>, options: RetryOptions<T>): Promise<T>;
|
|
658
|
+
defaults: {
|
|
659
|
+
retry: number;
|
|
660
|
+
retryBackOff: "exponential";
|
|
661
|
+
retryDelay: number;
|
|
662
|
+
retryDelayJitter: true;
|
|
663
|
+
retryDelayJitterMax: number;
|
|
664
|
+
};
|
|
665
|
+
};
|
|
613
666
|
|
|
614
|
-
export { type
|
|
667
|
+
export { type DeferredAsyncCallback, type DeferredAsyncDefaults, type DeferredAsyncGetPromise, type DeferredAsyncOptions, type IPromisE, type IPromisE_Delay, type IPromisE_Timeout, type OnEarlyFinalize, PromisE, PromisEBase, type PromiseParams, ResolveError, ResolveIgnored, type RetryOptions, type TimeoutFunc, type TimeoutOptions, PromisE as default, deferred, deferredCallback, delay, delayReject, retry, timeout };
|
package/dist/index.js
CHANGED
|
@@ -1,46 +1,10 @@
|
|
|
1
|
-
// src/types/deferred.ts
|
|
2
|
-
var ResolveError = /* @__PURE__ */ ((ResolveError2) => {
|
|
3
|
-
ResolveError2["NEVER"] = "NEVER";
|
|
4
|
-
ResolveError2["REJECT"] = "REJECT";
|
|
5
|
-
ResolveError2["WITH_ERROR"] = "RESOLVE_ERROR";
|
|
6
|
-
ResolveError2["WITH_UNDEFINED"] = "RESOLVE_UNDEFINED";
|
|
7
|
-
return ResolveError2;
|
|
8
|
-
})(ResolveError || {});
|
|
9
|
-
var ResolveIgnored = /* @__PURE__ */ ((ResolveIgnored2) => {
|
|
10
|
-
ResolveIgnored2["NEVER"] = "NEVER";
|
|
11
|
-
ResolveIgnored2["WITH_LAST"] = "WITH_LAST";
|
|
12
|
-
ResolveIgnored2["WITH_UNDEFINED"] = "WITH_UNDEFINED";
|
|
13
|
-
return ResolveIgnored2;
|
|
14
|
-
})(ResolveIgnored || {});
|
|
15
|
-
|
|
16
|
-
// src/config.ts
|
|
17
|
-
var config = {
|
|
18
|
-
/** Default value for `options` used by `PromisE.*deferred*` functions */
|
|
19
|
-
deferOptions: {
|
|
20
|
-
delayMs: 100,
|
|
21
|
-
resolveError: "REJECT" /* REJECT */,
|
|
22
|
-
resolveIgnored: "WITH_LAST" /* WITH_LAST */,
|
|
23
|
-
throttle: false
|
|
24
|
-
},
|
|
25
|
-
delayTimeoutMsg: "Timed out after",
|
|
26
|
-
retryOptions: {
|
|
27
|
-
retry: 1,
|
|
28
|
-
retryBackOff: "exponential",
|
|
29
|
-
retryDelay: 300,
|
|
30
|
-
retryDelayJitter: true,
|
|
31
|
-
retryDelayJitterMax: 100,
|
|
32
|
-
retryIf: null
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
var config_default = config;
|
|
36
|
-
|
|
37
1
|
// src/deferred.ts
|
|
38
2
|
import {
|
|
39
3
|
deferred as deferredCore,
|
|
40
4
|
fallbackIfFails as fallbackIfFails2,
|
|
41
|
-
forceCast,
|
|
42
5
|
isFn as isFn2,
|
|
43
6
|
isPositiveNumber,
|
|
7
|
+
objCopy,
|
|
44
8
|
throttled as throttledCore
|
|
45
9
|
} from "@superutils/core";
|
|
46
10
|
|
|
@@ -185,17 +149,34 @@ _PromisEBase.withResolvers = () => {
|
|
|
185
149
|
var PromisEBase = _PromisEBase;
|
|
186
150
|
var PromisEBase_default = PromisEBase;
|
|
187
151
|
|
|
152
|
+
// src/types/deferred.ts
|
|
153
|
+
var ResolveError = /* @__PURE__ */ ((ResolveError2) => {
|
|
154
|
+
ResolveError2["NEVER"] = "NEVER";
|
|
155
|
+
ResolveError2["REJECT"] = "REJECT";
|
|
156
|
+
ResolveError2["WITH_ERROR"] = "RESOLVE_ERROR";
|
|
157
|
+
ResolveError2["WITH_UNDEFINED"] = "RESOLVE_UNDEFINED";
|
|
158
|
+
return ResolveError2;
|
|
159
|
+
})(ResolveError || {});
|
|
160
|
+
var ResolveIgnored = /* @__PURE__ */ ((ResolveIgnored2) => {
|
|
161
|
+
ResolveIgnored2["NEVER"] = "NEVER";
|
|
162
|
+
ResolveIgnored2["WITH_LAST"] = "WITH_LAST";
|
|
163
|
+
ResolveIgnored2["WITH_UNDEFINED"] = "WITH_UNDEFINED";
|
|
164
|
+
return ResolveIgnored2;
|
|
165
|
+
})(ResolveIgnored || {});
|
|
166
|
+
|
|
188
167
|
// src/deferred.ts
|
|
189
|
-
function deferred(options
|
|
190
|
-
const defaults =
|
|
168
|
+
function deferred(options) {
|
|
169
|
+
const { defaults } = deferred;
|
|
170
|
+
options = objCopy(defaults, options, [], "empty");
|
|
191
171
|
let { onError, onIgnore, onResult } = options;
|
|
192
172
|
const {
|
|
193
|
-
|
|
194
|
-
|
|
173
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
174
|
+
delayMs,
|
|
175
|
+
resolveError,
|
|
195
176
|
// by default reject on error
|
|
196
|
-
resolveIgnored
|
|
177
|
+
resolveIgnored,
|
|
197
178
|
thisArg,
|
|
198
|
-
throttle
|
|
179
|
+
throttle
|
|
199
180
|
} = options;
|
|
200
181
|
let lastPromisE = null;
|
|
201
182
|
const queue = /* @__PURE__ */ new Map();
|
|
@@ -273,10 +254,15 @@ function deferred(options = {}) {
|
|
|
273
254
|
qItem.started = false;
|
|
274
255
|
queue.set(id, qItem);
|
|
275
256
|
if (gotDelay || !lastPromisE) execute(id, qItem);
|
|
276
|
-
return
|
|
257
|
+
return qItem;
|
|
277
258
|
};
|
|
278
259
|
return deferredFunc;
|
|
279
260
|
}
|
|
261
|
+
deferred.defaults = {
|
|
262
|
+
delayMs: -100,
|
|
263
|
+
resolveError: "REJECT" /* REJECT */,
|
|
264
|
+
resolveIgnored: "WITH_LAST" /* WITH_LAST */
|
|
265
|
+
};
|
|
280
266
|
var deferred_default = deferred;
|
|
281
267
|
|
|
282
268
|
// src/deferredCallback.ts
|
|
@@ -290,7 +276,7 @@ var deferredCallback_default = deferredCallback;
|
|
|
290
276
|
|
|
291
277
|
// src/delay.ts
|
|
292
278
|
import { fallbackIfFails as fallbackIfFails3, isFn as isFn3 } from "@superutils/core";
|
|
293
|
-
function delay(duration =
|
|
279
|
+
function delay(duration = delay.defaults.duration, result = duration, asRejected = false) {
|
|
294
280
|
const promise = new PromisEBase_default();
|
|
295
281
|
const finalize = (result2) => {
|
|
296
282
|
var _a;
|
|
@@ -298,7 +284,7 @@ function delay(duration = 100, result = duration, asRejected = false) {
|
|
|
298
284
|
result2 = (_a = fallbackIfFails3(result2, [], void 0)) != null ? _a : duration;
|
|
299
285
|
if (!asRejected) return promise.resolve(result2);
|
|
300
286
|
promise.reject(
|
|
301
|
-
result2 != null ? result2 : new Error(`${
|
|
287
|
+
result2 != null ? result2 : new Error(`${delay.defaults.delayTimeoutMsg} ${duration}ms`)
|
|
302
288
|
);
|
|
303
289
|
};
|
|
304
290
|
promise.timeoutId = setTimeout(() => finalize(result), duration);
|
|
@@ -307,6 +293,12 @@ function delay(duration = 100, result = duration, asRejected = false) {
|
|
|
307
293
|
}).finally(() => promise.pause());
|
|
308
294
|
return promise;
|
|
309
295
|
}
|
|
296
|
+
delay.defaults = {
|
|
297
|
+
/** Default delay duration in milliseconds */
|
|
298
|
+
duration: 100,
|
|
299
|
+
/** Default timed out message */
|
|
300
|
+
delayTimeoutMsg: "Timed out after"
|
|
301
|
+
};
|
|
310
302
|
var delay_default = delay;
|
|
311
303
|
|
|
312
304
|
// src/delayReject.ts
|
|
@@ -316,42 +308,65 @@ function delayReject(duration, reason) {
|
|
|
316
308
|
var delayReject_default = delayReject;
|
|
317
309
|
|
|
318
310
|
// src/retry.ts
|
|
319
|
-
import {
|
|
320
|
-
|
|
321
|
-
|
|
311
|
+
import {
|
|
312
|
+
fallbackIfFails as fallbackIfFails4,
|
|
313
|
+
isEmpty,
|
|
314
|
+
isPositiveInteger,
|
|
315
|
+
objCopy as objCopy2
|
|
316
|
+
} from "@superutils/core";
|
|
317
|
+
var retry = async (func, options) => {
|
|
318
|
+
options = objCopy2(retry.defaults, options != null ? options : {}, [], (key, value) => {
|
|
319
|
+
switch (key) {
|
|
320
|
+
// case 'retryDelayJitter':
|
|
321
|
+
// return true
|
|
322
|
+
case "retry":
|
|
323
|
+
// eslint-disable-next-line no-fallthrough
|
|
324
|
+
case "retryDelay":
|
|
325
|
+
case "retryDelayJitterMax":
|
|
326
|
+
return value !== 0 && !isPositiveInteger(value);
|
|
327
|
+
}
|
|
328
|
+
return !!isEmpty(value);
|
|
329
|
+
});
|
|
322
330
|
const {
|
|
323
|
-
|
|
324
|
-
retryBackOff
|
|
325
|
-
|
|
331
|
+
retry: maxRetries,
|
|
332
|
+
retryBackOff,
|
|
333
|
+
retryDelay,
|
|
334
|
+
retryDelayJitter,
|
|
335
|
+
retryDelayJitterMax
|
|
326
336
|
} = options;
|
|
327
|
-
let
|
|
328
|
-
retry: maxRetries = d.retry,
|
|
329
|
-
retryDelay: delayMs,
|
|
330
|
-
retryDelayJitterMax: jitterMax
|
|
331
|
-
} = options;
|
|
332
|
-
if (maxRetries !== 0 && !isPositiveInteger(maxRetries)) maxRetries = d.retry;
|
|
333
|
-
if (!isPositiveInteger(delayMs)) delayMs = d.retryDelay;
|
|
334
|
-
if (!isPositiveInteger(jitterMax)) jitterMax = d.retryDelayJitterMax;
|
|
337
|
+
let _retryDelay = retryDelay;
|
|
335
338
|
let retryCount = -1;
|
|
336
339
|
let result;
|
|
337
340
|
let error;
|
|
338
341
|
let shouldRetry = false;
|
|
339
342
|
do {
|
|
340
343
|
retryCount++;
|
|
341
|
-
if (retryBackOff === "exponential" && retryCount > 1)
|
|
342
|
-
if (retryDelayJitter)
|
|
343
|
-
|
|
344
|
+
if (retryBackOff === "exponential" && retryCount > 1) _retryDelay *= 2;
|
|
345
|
+
if (retryDelayJitter)
|
|
346
|
+
_retryDelay += Math.floor(Math.random() * retryDelayJitterMax);
|
|
347
|
+
retryCount > 0 && await delay_default(_retryDelay);
|
|
344
348
|
try {
|
|
345
349
|
error = void 0;
|
|
346
350
|
result = await func();
|
|
347
351
|
} catch (err) {
|
|
348
352
|
error = err;
|
|
349
353
|
}
|
|
350
|
-
shouldRetry = maxRetries > 0 && retryCount < maxRetries && (!!error || !!
|
|
354
|
+
shouldRetry = maxRetries > 0 && retryCount < maxRetries && (!!error || !!await fallbackIfFails4(
|
|
355
|
+
options.retryIf,
|
|
356
|
+
[result, retryCount, error],
|
|
357
|
+
false
|
|
358
|
+
));
|
|
351
359
|
} while (shouldRetry);
|
|
352
360
|
if (error !== void 0) return Promise.reject(error);
|
|
353
361
|
return result;
|
|
354
362
|
};
|
|
363
|
+
retry.defaults = {
|
|
364
|
+
retry: 1,
|
|
365
|
+
retryBackOff: "exponential",
|
|
366
|
+
retryDelay: 300,
|
|
367
|
+
retryDelayJitter: true,
|
|
368
|
+
retryDelayJitterMax: 100
|
|
369
|
+
};
|
|
355
370
|
var retry_default = retry;
|
|
356
371
|
|
|
357
372
|
// src/timeout.ts
|
|
@@ -397,8 +412,6 @@ var timeout_default = timeout;
|
|
|
397
412
|
// src/PromisE.ts
|
|
398
413
|
var PromisE = class extends PromisEBase_default {
|
|
399
414
|
};
|
|
400
|
-
/** Global configuration & default values */
|
|
401
|
-
PromisE.config = config_default;
|
|
402
415
|
PromisE.deferred = deferred_default;
|
|
403
416
|
PromisE.deferredCallback = deferredCallback_default;
|
|
404
417
|
PromisE.delay = delay_default;
|
|
@@ -414,7 +427,6 @@ export {
|
|
|
414
427
|
PromisEBase,
|
|
415
428
|
ResolveError,
|
|
416
429
|
ResolveIgnored,
|
|
417
|
-
config,
|
|
418
430
|
index_default as default,
|
|
419
431
|
deferred,
|
|
420
432
|
deferredCallback,
|
package/package.json
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
"url": "https://github.com/alien45/superutils/issues"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@superutils/core": "1.0.
|
|
7
|
+
"@superutils/core": "^1.0.5"
|
|
8
8
|
},
|
|
9
|
-
"description": "An extended Promise
|
|
9
|
+
"description": "An extended Promise with extra features such as status tracking, deferred/throttled execution, timeout and retry mechanism.",
|
|
10
10
|
"files": [
|
|
11
11
|
"dist",
|
|
12
12
|
"README.md",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"main": "dist/index.js",
|
|
24
24
|
"name": "@superutils/promise",
|
|
25
25
|
"peerDpendencies": {
|
|
26
|
-
"@superutils/core": "1.0.
|
|
26
|
+
"@superutils/core": "^1.0.5"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
@@ -37,10 +37,11 @@
|
|
|
37
37
|
"_watch": "tsc -p tsconfig.json --watch",
|
|
38
38
|
"build": "tsup src/index.ts --format esm --dts --clean --config ../../tsup.config.js",
|
|
39
39
|
"dev": "npm run build -- --watch",
|
|
40
|
+
"start": "npm run build -- --watch",
|
|
40
41
|
"test": "cd ../../ && npm run test promise"
|
|
41
42
|
},
|
|
42
43
|
"sideEffects": false,
|
|
43
44
|
"type": "module",
|
|
44
45
|
"types": "dist/index.d.ts",
|
|
45
|
-
"version": "1.0.
|
|
46
|
+
"version": "1.0.5"
|
|
46
47
|
}
|