@@ -10,153 +10,173 @@ enum States {
10
10
REJECTED = 'REJECTED' ,
11
11
}
12
12
13
- /** JSDoc */
14
- interface Handler < T , U > {
15
- onFail : HandlerOnFail < U > ;
16
- onSuccess : HandlerOnSuccess < T , U > ;
17
- }
18
-
19
- type HandlerOnSuccess < T , U = any > = ( value : T ) => U | Thenable < U > ;
20
- type HandlerOnFail < U = any > = ( reason : any ) => U | Thenable < U > ;
21
-
22
- /** JSDoc */
23
- interface Thenable < T > {
24
- /** JSDoc */
25
- then < U > ( onSuccess ?: HandlerOnSuccess < T , U > , onFail ?: HandlerOnFail < U > | ( ( reason : any ) => void ) ) : Thenable < U > ;
26
- }
27
-
28
- type Resolve < R > = ( value ?: R | Thenable < R > | any ) => void ;
29
- type Reject = ( value ?: any ) => void ;
30
-
31
- /** JSDoc */
32
- export class SyncPromise < T > implements PromiseLike < T > {
33
- /** JSDoc */
13
+ // declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => PromiseLike<T>;
14
+
15
+ // interface PromiseLike<T> {
16
+ // /**
17
+ // * Attaches callbacks for the resolution and/or rejection of the Promise.
18
+ // * @param onfulfilled The callback to execute when the Promise is resolved.
19
+ // * @param onrejected The callback to execute when the Promise is rejected.
20
+ // * @returns A Promise for the completion of which ever callback is executed.
21
+ // */
22
+ // then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
23
+ // }
24
+
25
+ // /**
26
+ // * Represents the completion of an asynchronous operation
27
+ // */
28
+ // interface Promise<T> {
29
+ // /**
30
+ // * Attaches callbacks for the resolution and/or rejection of the Promise.
31
+ // * @param onfulfilled The callback to execute when the Promise is resolved.
32
+ // * @param onrejected The callback to execute when the Promise is rejected.
33
+ // * @returns A Promise for the completion of which ever callback is executed.
34
+ // */
35
+ // then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
36
+
37
+ // /**
38
+ // * Attaches a callback for only the rejection of the Promise.
39
+ // * @param onrejected The callback to execute when the Promise is rejected.
40
+ // * @returns A Promise for the completion of the callback.
41
+ // */
42
+ // catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
43
+ // }
44
+
45
+ // interface Handler<TResult1 = T, TResult2> {
46
+ // onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null;
47
+ // onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null;
48
+ // }
49
+ export class SyncPromise < T > implements Promise < T > {
34
50
private _state : States = States . PENDING ;
35
- /** JSDoc */
36
- private _handlers : Array < Handler < T , any > > = [ ] ;
37
- /** JSDoc */
38
- private _value : T | any ;
51
+ // private _handlers: Array<Handler<T, any>> = [];
52
+ private _value : any ;
39
53
40
- public constructor ( callback : ( resolve : Resolve < T > , reject : Reject ) => void ) {
54
+ public constructor (
55
+ executor : ( resolve : ( value ?: T | PromiseLike < T > ) => void , reject : ( reason ?: any ) => void ) => void ,
56
+ ) {
41
57
try {
42
- callback ( this . _resolve , this . _reject ) ;
58
+ executor ( this . _resolve , this . _reject ) ;
43
59
} catch ( e ) {
44
60
this . _reject ( e ) ;
45
61
}
46
62
}
47
63
48
- /** JSDoc */
49
- private readonly _resolve = ( value : T ) => {
50
- this . _setResult ( value , States . RESOLVED ) ;
51
- } ;
52
-
53
- /** JSDoc */
54
- private readonly _reject = ( reason : any ) => {
55
- this . _setResult ( reason , States . REJECTED ) ;
56
- } ;
57
-
58
- /** JSDoc */
59
- private readonly _setResult = ( value : T | any , state : States ) => {
60
- if ( this . _state !== States . PENDING ) {
61
- return ;
62
- }
63
-
64
- if ( isThenable ( value ) ) {
65
- ( value as Thenable < T > ) . then ( this . _resolve , this . _reject ) ;
66
- return ;
67
- }
68
-
69
- this . _value = value ;
70
- this . _state = state ;
71
-
72
- this . _executeHandlers ( ) ;
73
- } ;
64
+ public toString ( ) : string {
65
+ return '[object SyncPromise]' ;
66
+ }
74
67
75
- /** JSDoc */
76
- private readonly _executeHandlers = ( ) => {
77
- if ( this . _state === States . PENDING ) {
78
- return ;
79
- }
68
+ public [ Symbol . toStringTag ] : string = '[object SyncPromise]' ;
80
69
81
- if ( this . _state === States . REJECTED ) {
82
- // tslint:disable-next-line:no-unsafe-any
83
- this . _handlers . forEach ( h => h . onFail && h . onFail ( this . _value ) ) ;
84
- } else {
85
- // tslint:disable-next-line:no-unsafe-any
86
- this . _handlers . forEach ( h => h . onSuccess && h . onSuccess ( this . _value ) ) ;
87
- }
70
+ // resolve<T>(value: T | PromiseLike<T>): Promise<T>;
71
+ // resolve(): Promise<void>;
88
72
89
- this . _handlers = [ ] ;
90
- return ;
91
- } ;
73
+ public static resolve < T > ( value : T | PromiseLike < T > ) : Promise < T > {
74
+ return new Promise ( ( resolve , reject ) => { } ) ;
75
+ // return new SyncPromise<U>(resolve => {
76
+ // resolve(value);
77
+ // });
78
+ }
92
79
93
- /** JSDoc */
94
- private readonly _attachHandler = ( handler : Handler < T , any > ) => {
95
- this . _handlers = this . _handlers . concat ( handler ) ;
96
- this . _executeHandlers ( ) ;
97
- } ;
80
+ public static reject < T = never > ( reason ?: any ) : Promise < T > {
81
+ return new Promise ( ( resolve , reject ) => { } ) ;
82
+ // return new SyncPromise<U>((_, reject) => {
83
+ // reject(reason);
84
+ // });
85
+ }
98
86
99
- /** JSDoc */
100
87
public then < TResult1 = T , TResult2 = never > (
101
- onfulfilled ?: ( ( value : T ) => TResult1 | PromiseLike < TResult1 > ) | undefined ,
102
- onrejected ?: ( ( reason : any ) => TResult2 | PromiseLike < TResult2 > ) | undefined ,
103
- ) : SyncPromise < TResult1 | TResult2 > {
104
- // public then<U>(onSuccess?: HandlerOnSuccess<T, U>, onFail?: HandlerOnFail<U>): SyncPromise<T | U > {
105
- return new SyncPromise < TResult1 | TResult2 > ( ( resolve , reject ) => {
106
- this . _attachHandler ( {
107
- onFail : reason => {
108
- if ( ! onrejected ) {
109
- reject ( reason ) ;
110
- return ;
111
- }
112
-
113
- try {
114
- resolve ( onrejected ( reason ) ) ;
115
- return ;
116
- } catch ( e ) {
117
- reject ( e ) ;
118
- return ;
119
- }
120
- } ,
121
- onSuccess : result => {
122
- if ( ! onfulfilled ) {
123
- resolve ( result ) ;
124
- return ;
125
- }
126
- try {
127
- resolve ( onfulfilled ( result ) ) ;
128
- return ;
129
- } catch ( e ) {
130
- reject ( e ) ;
131
- return ;
132
- }
133
- } ,
134
- } ) ;
135
- } ) ;
88
+ onfulfilled ?: ( ( value : T ) => TResult1 | PromiseLike < TResult1 > ) | undefined | null ,
89
+ onrejected ?: ( ( reason : any ) => TResult2 | PromiseLike < TResult2 > ) | undefined | null ,
90
+ ) : Promise < TResult1 | TResult2 > {
91
+ return new Promise ( ( resolve , reject ) = > { } ) ;
92
+
93
+ // return new SyncPromise<TResult1 | TResult2>((resolve, reject) => {
94
+ // this._attachHandler( {
95
+ // onFail: reason => {
96
+ // if (!onrejected) {
97
+ // reject(reason) ;
98
+ // return;
99
+ // }
100
+ // try {
101
+ // resolve(onrejected(reason));
102
+ // return;
103
+ // } catch (e) {
104
+ // reject(e);
105
+ // return;
106
+ // }
107
+ // },
108
+ // onSuccess: result => {
109
+ // if (!onfulfilled) {
110
+ // resolve(result);
111
+ // return;
112
+ // }
113
+ // try {
114
+ // resolve(onfulfilled(result));
115
+ // return;
116
+ // } catch (e) {
117
+ // reject(e);
118
+ // return;
119
+ // }
120
+ // },
121
+ // });
122
+ // });
136
123
}
137
124
138
- /** JSDoc */
139
- public catch < U > ( onFail : HandlerOnFail < U > ) : SyncPromise < U > {
125
+ public catch < TResult = never > (
126
+ onrejected ?: ( ( reason : any ) => TResult | PromiseLike < TResult > ) | undefined | null ,
127
+ ) : Promise < T | TResult > {
128
+ return new Promise ( ( resolve , reject ) => { } ) ;
140
129
// tslint:disable-next-line:no-unsafe-any
141
- return this . then < U > ( ( val : any ) => val , onFail as any ) ;
130
+ // return this.then<U>((val: any) => val, onFail as any);
142
131
}
143
132
144
- /** JSDoc */
145
- public toString ( ) : string {
146
- return `[object SyncPromise]` ;
133
+ public finally ( onfinally ?: ( ( ) => void ) | undefined | null ) : Promise < T > {
134
+ return new Promise ( ( resolve , reject ) => { } ) ;
147
135
}
148
136
149
- /** JSDoc */
150
- public static resolve < U > ( value ?: U | Thenable < U > ) : SyncPromise < U > {
151
- return new SyncPromise < U > ( resolve => {
152
- resolve ( value ) ;
153
- } ) ;
137
+ private _resolve ( value ?: T | PromiseLike < T > ) : void {
138
+ // this._setResult(value, States.RESOLVED);
154
139
}
155
140
156
- /** JSDoc */
157
- public static reject < U > ( reason ?: any ) : SyncPromise < U > {
158
- return new SyncPromise < U > ( ( _ , reject ) => {
159
- reject ( reason ) ;
160
- } ) ;
141
+ private _reject ( reason ?: any ) : void {
142
+ // this._setResult(reason, States.REJECTED);
161
143
}
144
+
145
+ // private readonly _setResult = (value: T | any, state: States) => {
146
+ // if (this._state !== States.PENDING) {
147
+ // return;
148
+ // }
149
+
150
+ // if (isThenable(value)) {
151
+ // (value as Thenable<T>).then(this._resolve, this._reject);
152
+ // return;
153
+ // }
154
+
155
+ // this._value = value;
156
+ // this._state = state;
157
+
158
+ // this._executeHandlers();
159
+ // };
160
+
161
+ // private readonly _executeHandlers = () => {
162
+ // if (this._state === States.PENDING) {
163
+ // return;
164
+ // }
165
+
166
+ // if (this._state === States.REJECTED) {
167
+ // // tslint:disable-next-line:no-unsafe-any
168
+ // this._handlers.forEach(h => h.onFail && h.onFail(this._value));
169
+ // } else {
170
+ // // tslint:disable-next-line:no-unsafe-any
171
+ // this._handlers.forEach(h => h.onSuccess && h.onSuccess(this._value));
172
+ // }
173
+
174
+ // this._handlers = [];
175
+ // return;
176
+ // };
177
+
178
+ // private readonly _attachHandler = (handler: Handler<T, any>) => {
179
+ // this._handlers = this._handlers.concat(handler);
180
+ // this._executeHandlers();
181
+ // };
162
182
}
0 commit comments