@superutils/promise 1.0.6 → 1.0.9
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 +1 -1
- package/dist/index.d.ts +12 -5
- package/dist/index.js +19 -10
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
An extended `Promise` implementation, named `PromisE`, that provides additional features and utilities for easier asynchronous flow control in JavaScript and TypeScript applications.
|
|
4
4
|
|
|
5
|
-
This package offers a drop-in replacement for the native `Promise` that includes status tracking (`.pending`, `.resolved`, `.rejected`) and a suite of
|
|
5
|
+
This package offers a drop-in replacement for the native `Promise` that includes status tracking (`.pending`, `.resolved`, `.rejected`) and a suite of practical static methods for common asynchronous patterns like deferred execution, throttling, and cancellable fetches.
|
|
6
6
|
|
|
7
7
|
<div v-if="false">
|
|
8
8
|
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ interface IPromisE<T = unknown> extends Promise<T> {
|
|
|
17
17
|
/** Indicates if the promise has been resolved */
|
|
18
18
|
readonly resolved: boolean;
|
|
19
19
|
}
|
|
20
|
-
interface IPromisE_Delay<T = unknown> extends IPromisE<T> {
|
|
20
|
+
interface IPromisE_Delay<T = unknown> extends Promise<T>, IPromisE<T> {
|
|
21
21
|
/**
|
|
22
22
|
* Caution: pausing will prevent the promise from resolving/rejeting automatically.
|
|
23
23
|
*
|
|
@@ -58,6 +58,7 @@ interface IPromisE_Delay<T = unknown> extends IPromisE<T> {
|
|
|
58
58
|
* ```
|
|
59
59
|
*/
|
|
60
60
|
pause: () => void;
|
|
61
|
+
/** Timeout ID */
|
|
61
62
|
timeoutId: TimeoutId;
|
|
62
63
|
}
|
|
63
64
|
/**
|
|
@@ -122,8 +123,12 @@ type DeferredAsyncOptions<ThisArg = unknown, DelayMs extends number = number> =
|
|
|
122
123
|
} & Omit<ThrottleOptions, 'onError' | 'ThisArg' | 'tid'>) | ({
|
|
123
124
|
/** Debounce/deferred duration in milliseconds */
|
|
124
125
|
delayMs?: PositiveNumber<DelayMs>;
|
|
125
|
-
throttle?: false
|
|
126
|
-
} & Omit<DeferredOptions, 'onError' | 'ThisArg' | 'tid'>)
|
|
126
|
+
throttle?: false;
|
|
127
|
+
} & Omit<DeferredOptions, 'onError' | 'ThisArg' | 'tid'>) | {
|
|
128
|
+
/** Sequential execution. All promises will be executed sequentially making sure there is no overlap. */
|
|
129
|
+
delayMs: 0;
|
|
130
|
+
throttle?: false;
|
|
131
|
+
});
|
|
127
132
|
/** Determines what to do when deferred promise/function fails */
|
|
128
133
|
declare enum ResolveError {
|
|
129
134
|
/** Neither resolve nor reject the failed */
|
|
@@ -142,7 +147,7 @@ declare enum ResolveError {
|
|
|
142
147
|
declare enum ResolveIgnored {
|
|
143
148
|
/** Never resolve ignored promises. Caution: make sure this doesn't cause any memory leaks. */
|
|
144
149
|
NEVER = "NEVER",
|
|
145
|
-
/** (default) resolve with active promise result, the one that caused the current promise/callback to be ignored
|
|
150
|
+
/** (default) resolve with active promise result, the one that caused the current promise/callback to be ignored. */
|
|
146
151
|
WITH_LAST = "WITH_LAST",
|
|
147
152
|
/** resolve with `undefined` value */
|
|
148
153
|
WITH_UNDEFINED = "WITH_UNDEFINED"
|
|
@@ -188,9 +193,9 @@ type RetryOptions<T = unknown> = {
|
|
|
188
193
|
};
|
|
189
194
|
|
|
190
195
|
declare class PromisEBase<T = unknown> extends Promise<T> implements IPromisE<T> {
|
|
191
|
-
readonly state: 0 | 1 | 2;
|
|
192
196
|
private _resolve?;
|
|
193
197
|
private _reject?;
|
|
198
|
+
private _state;
|
|
194
199
|
/**
|
|
195
200
|
* callbacks to be invoked whenever PromisE instance is finalized early using non-static resolve()/reject() methods */
|
|
196
201
|
onEarlyFinalize: OnEarlyFinalize<T>[];
|
|
@@ -219,6 +224,8 @@ declare class PromisEBase<T = unknown> extends Promise<T> implements IPromisE<T>
|
|
|
219
224
|
get rejected(): boolean;
|
|
220
225
|
/** Indicates if the promise has been resolved */
|
|
221
226
|
get resolved(): boolean;
|
|
227
|
+
/** Get promise status code */
|
|
228
|
+
get state(): 0 | 1 | 2;
|
|
222
229
|
/** Resovle pending promise early. */
|
|
223
230
|
resolve: (value: T | PromiseLike<T>) => void;
|
|
224
231
|
/** Reject pending promise early. */
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
} from "@superutils/core";
|
|
10
10
|
|
|
11
11
|
// src/PromisEBase.ts
|
|
12
|
-
import {
|
|
12
|
+
import { fallbackIfFails, isFn, isPromise } from "@superutils/core";
|
|
13
13
|
var _PromisEBase = class _PromisEBase extends Promise {
|
|
14
14
|
constructor(input) {
|
|
15
15
|
if (input instanceof _PromisEBase) return input;
|
|
@@ -17,11 +17,11 @@ var _PromisEBase = class _PromisEBase extends Promise {
|
|
|
17
17
|
let _reject;
|
|
18
18
|
super((resolve, reject) => {
|
|
19
19
|
_reject = (reason) => {
|
|
20
|
-
|
|
20
|
+
this._state = 2;
|
|
21
21
|
reject(reason);
|
|
22
22
|
};
|
|
23
23
|
_resolve = (value) => {
|
|
24
|
-
|
|
24
|
+
this._state = 1;
|
|
25
25
|
resolve(value);
|
|
26
26
|
};
|
|
27
27
|
input != null ? input : input = () => {
|
|
@@ -29,7 +29,7 @@ var _PromisEBase = class _PromisEBase extends Promise {
|
|
|
29
29
|
const promise = isPromise(input) ? input : isFn(input) ? new globalThis.Promise(input) : Promise.resolve(input);
|
|
30
30
|
promise.then(_resolve, _reject);
|
|
31
31
|
});
|
|
32
|
-
this.
|
|
32
|
+
this._state = 0;
|
|
33
33
|
/**
|
|
34
34
|
* callbacks to be invoked whenever PromisE instance is finalized early using non-static resolve()/reject() methods */
|
|
35
35
|
this.onEarlyFinalize = [];
|
|
@@ -66,15 +66,19 @@ var _PromisEBase = class _PromisEBase extends Promise {
|
|
|
66
66
|
//
|
|
67
67
|
/** Indicates if the promise is still pending/unfinalized */
|
|
68
68
|
get pending() {
|
|
69
|
-
return this.
|
|
69
|
+
return this._state === 0;
|
|
70
70
|
}
|
|
71
71
|
/** Indicates if the promise has been rejected */
|
|
72
72
|
get rejected() {
|
|
73
|
-
return this.
|
|
73
|
+
return this._state === 2;
|
|
74
74
|
}
|
|
75
75
|
/** Indicates if the promise has been resolved */
|
|
76
76
|
get resolved() {
|
|
77
|
-
return this.
|
|
77
|
+
return this._state === 1;
|
|
78
|
+
}
|
|
79
|
+
/** Get promise status code */
|
|
80
|
+
get state() {
|
|
81
|
+
return this._state;
|
|
78
82
|
}
|
|
79
83
|
// static withResolvers = <T = unknown>() => {
|
|
80
84
|
// const pwr = globalThis.Promise.withResolvers<T>()
|
|
@@ -165,9 +169,14 @@ var ResolveIgnored = /* @__PURE__ */ ((ResolveIgnored2) => {
|
|
|
165
169
|
})(ResolveIgnored || {});
|
|
166
170
|
|
|
167
171
|
// src/deferred.ts
|
|
168
|
-
function deferred(options) {
|
|
172
|
+
function deferred(options = {}) {
|
|
169
173
|
const { defaults } = deferred;
|
|
170
|
-
options = objCopy(
|
|
174
|
+
options = objCopy(
|
|
175
|
+
defaults,
|
|
176
|
+
options,
|
|
177
|
+
[],
|
|
178
|
+
"empty"
|
|
179
|
+
);
|
|
171
180
|
let { onError, onIgnore, onResult } = options;
|
|
172
181
|
const {
|
|
173
182
|
delayMs,
|
|
@@ -247,7 +256,7 @@ function deferred(options) {
|
|
|
247
256
|
return deferFn(execute2, delayMs, options);
|
|
248
257
|
})();
|
|
249
258
|
const deferredFunc = (promise) => {
|
|
250
|
-
const id = Symbol("deferred-queue-item-id");
|
|
259
|
+
const id = /* @__PURE__ */ Symbol("deferred-queue-item-id");
|
|
251
260
|
const qItem = new PromisEBase_default();
|
|
252
261
|
qItem.getPromise = isFn2(promise) ? promise : () => promise;
|
|
253
262
|
qItem.started = false;
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"url": "https://github.com/alien45/superutils/issues"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@superutils/core": "^1.0.
|
|
7
|
+
"@superutils/core": "^1.0.8"
|
|
8
8
|
},
|
|
9
9
|
"description": "An extended Promise with extra features such as status tracking, deferred/throttled execution, timeout and retry mechanism.",
|
|
10
10
|
"files": [
|
|
@@ -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.8"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"sideEffects": false,
|
|
44
44
|
"type": "module",
|
|
45
45
|
"types": "dist/index.d.ts",
|
|
46
|
-
"version": "1.0.
|
|
46
|
+
"version": "1.0.9"
|
|
47
47
|
}
|