@trenskow/custom-promise 0.10.2 → 0.11.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/index.js +56 -19
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -4,19 +4,17 @@ class CustomPromise {
|
|
|
4
4
|
|
|
5
5
|
constructor() {
|
|
6
6
|
this._state = 'pending';
|
|
7
|
-
this.
|
|
7
|
+
this._result = undefined;
|
|
8
|
+
this._resolveCallback = undefined;
|
|
9
|
+
this._rejectCallback = undefined;
|
|
8
10
|
this._finalizers = [];
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
_reset() {
|
|
12
|
-
this._state = 'pending';
|
|
13
|
-
}
|
|
14
|
-
|
|
15
13
|
_resolve(result) {
|
|
16
14
|
if (this._state !== 'pending') return;
|
|
17
15
|
this._state = 'fulfilled';
|
|
18
16
|
this._result = result;
|
|
19
|
-
this.
|
|
17
|
+
this._resolveCallback?.(result);
|
|
20
18
|
this._finalizers.splice(0).forEach((onFinally) => onFinally());
|
|
21
19
|
}
|
|
22
20
|
|
|
@@ -24,25 +22,63 @@ class CustomPromise {
|
|
|
24
22
|
if (this._state !== 'pending') return;
|
|
25
23
|
this._state = 'rejected';
|
|
26
24
|
this._error = error;
|
|
27
|
-
this.
|
|
25
|
+
this._rejectCallback?.(error);
|
|
28
26
|
this._finalizers.splice(0).forEach((onFinally) => onFinally());
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
then(onFulfilled, onRejected) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
const resolveWrapper = (value) => {
|
|
33
|
+
try {
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
35
|
+
if (typeof onFulfilled !== 'function') {
|
|
36
|
+
return resolve(value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const result = onFulfilled(value);
|
|
40
|
+
|
|
41
|
+
if (typeof result?.then === 'function') {
|
|
42
|
+
return result.then(resolve, reject);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
resolve(result);
|
|
46
|
+
|
|
47
|
+
} catch (error) {
|
|
48
|
+
reject(error);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const rejectedWrapper = (error) => {
|
|
53
|
+
try {
|
|
54
|
+
|
|
55
|
+
if (typeof onRejected !== 'function') {
|
|
56
|
+
return reject(error);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const result = onRejected(error);
|
|
60
|
+
|
|
61
|
+
if (typeof result?.then === 'function') {
|
|
62
|
+
return result.then(resolve, reject);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
reject(result);
|
|
66
|
+
|
|
67
|
+
} catch (error) {
|
|
68
|
+
reject(error);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
if (this._state === 'fulfilled') {
|
|
73
|
+
resolveWrapper(this._result);
|
|
74
|
+
} else if (this._state === 'rejected') {
|
|
75
|
+
rejectedWrapper(this._error);
|
|
76
|
+
} else {
|
|
77
|
+
this._resolveCallback = resolveWrapper;
|
|
78
|
+
this._rejectCallback = rejectedWrapper;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
});
|
|
46
82
|
}
|
|
47
83
|
|
|
48
84
|
finally(onFinally) {
|
|
@@ -50,6 +86,7 @@ class CustomPromise {
|
|
|
50
86
|
else {
|
|
51
87
|
this._finalizers.push(onFinally);
|
|
52
88
|
}
|
|
89
|
+
return this;
|
|
53
90
|
}
|
|
54
91
|
|
|
55
92
|
}
|