22import { isThenable } from './is' ;
33
44/** SyncPromise internal states */
5- const enum States {
6- /** Pending */
7- PENDING = 0 ,
8- /** Resolved / OK */
9- RESOLVED = 1 ,
10- /** Rejected / Error */
11- REJECTED = 2 ,
12- }
5+ const STATE_PENDING = 0 ;
6+ const STATE_RESOLVED = 1 ;
7+ const STATE_REJECTED = 2 ;
8+
9+ type State = typeof STATE_PENDING | typeof STATE_RESOLVED | typeof STATE_REJECTED ;
1310
1411// Overloads so we can call resolvedSyncPromise without arguments and generic argument
1512export function resolvedSyncPromise ( ) : PromiseLike < void > ;
@@ -46,12 +43,12 @@ type Executor<T> = (resolve: (value?: T | PromiseLike<T> | null) => void, reject
4643 * but is not async internally
4744 */
4845export class SyncPromise < T > implements PromiseLike < T > {
49- private _state : States ;
46+ private _state : State ;
5047 private _handlers : Array < [ boolean , ( value : T ) => void , ( reason : any ) => any ] > ;
5148 private _value : any ;
5249
5350 public constructor ( executor : Executor < T > ) {
54- this . _state = States . PENDING ;
51+ this . _state = STATE_PENDING ;
5552 this . _handlers = [ ] ;
5653
5754 this . _runExecutor ( executor ) ;
@@ -135,7 +132,7 @@ export class SyncPromise<T> implements PromiseLike<T> {
135132
136133 /** Excute the resolve/reject handlers. */
137134 private _executeHandlers ( ) : void {
138- if ( this . _state === States . PENDING ) {
135+ if ( this . _state === STATE_PENDING ) {
139136 return ;
140137 }
141138
@@ -147,11 +144,11 @@ export class SyncPromise<T> implements PromiseLike<T> {
147144 return ;
148145 }
149146
150- if ( this . _state === States . RESOLVED ) {
147+ if ( this . _state === STATE_RESOLVED ) {
151148 handler [ 1 ] ( this . _value as unknown as any ) ;
152149 }
153150
154- if ( this . _state === States . REJECTED ) {
151+ if ( this . _state === STATE_REJECTED ) {
155152 handler [ 2 ] ( this . _value ) ;
156153 }
157154
@@ -161,8 +158,8 @@ export class SyncPromise<T> implements PromiseLike<T> {
161158
162159 /** Run the executor for the SyncPromise. */
163160 private _runExecutor ( executor : Executor < T > ) : void {
164- const setResult = ( state : States , value ?: T | PromiseLike < T > | any ) : void => {
165- if ( this . _state !== States . PENDING ) {
161+ const setResult = ( state : State , value ?: T | PromiseLike < T > | any ) : void => {
162+ if ( this . _state !== STATE_PENDING ) {
166163 return ;
167164 }
168165
@@ -178,11 +175,11 @@ export class SyncPromise<T> implements PromiseLike<T> {
178175 } ;
179176
180177 const resolve = ( value : unknown ) : void => {
181- setResult ( States . RESOLVED , value ) ;
178+ setResult ( STATE_RESOLVED , value ) ;
182179 } ;
183180
184181 const reject = ( reason : unknown ) : void => {
185- setResult ( States . REJECTED , reason ) ;
182+ setResult ( STATE_REJECTED , reason ) ;
186183 } ;
187184
188185 try {
0 commit comments