@superutils/promise 1.0.6 → 1.0.7
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 +5 -2
- package/dist/index.js +11 -7
- 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
|
/**
|
|
@@ -188,9 +189,9 @@ type RetryOptions<T = unknown> = {
|
|
|
188
189
|
};
|
|
189
190
|
|
|
190
191
|
declare class PromisEBase<T = unknown> extends Promise<T> implements IPromisE<T> {
|
|
191
|
-
readonly state: 0 | 1 | 2;
|
|
192
192
|
private _resolve?;
|
|
193
193
|
private _reject?;
|
|
194
|
+
private _state;
|
|
194
195
|
/**
|
|
195
196
|
* callbacks to be invoked whenever PromisE instance is finalized early using non-static resolve()/reject() methods */
|
|
196
197
|
onEarlyFinalize: OnEarlyFinalize<T>[];
|
|
@@ -219,6 +220,8 @@ declare class PromisEBase<T = unknown> extends Promise<T> implements IPromisE<T>
|
|
|
219
220
|
get rejected(): boolean;
|
|
220
221
|
/** Indicates if the promise has been resolved */
|
|
221
222
|
get resolved(): boolean;
|
|
223
|
+
/** Get promise status code */
|
|
224
|
+
get state(): 0 | 1 | 2;
|
|
222
225
|
/** Resovle pending promise early. */
|
|
223
226
|
resolve: (value: T | PromiseLike<T>) => void;
|
|
224
227
|
/** 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>()
|
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.7"
|
|
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.7"
|
|
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.7"
|
|
47
47
|
}
|