@@ -48,10 +48,12 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
4848 }
4949 } ;
5050
51+ const UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL = __symbol__ ( 'unhandledPromiseRejectionHandler' ) ;
52+
5153 function handleUnhandledRejection ( e : any ) {
5254 api . onUnhandledError ( e ) ;
5355 try {
54- const handler = ( Zone as any ) [ __symbol__ ( 'unhandledPromiseRejectionHandler' ) ] ;
56+ const handler = ( Zone as any ) [ UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL ] ;
5557 if ( handler && typeof handler === 'function' ) {
5658 handler . apply ( this , [ e ] ) ;
5759 }
@@ -104,18 +106,23 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
104106 } ;
105107 } ;
106108
109+ const TYPE_ERROR = 'Promise resolved with itself' ;
110+ const OBJECT = 'object' ;
111+ const FUNCTION = 'function' ;
112+ const CURRENT_TASK_SYMBOL = __symbol__ ( 'currentTask' ) ;
113+
107114 // Promise Resolution
108115 function resolvePromise (
109116 promise : ZoneAwarePromise < any > , state : boolean , value : any ) : ZoneAwarePromise < any > {
110117 const onceWrapper = once ( ) ;
111118 if ( promise === value ) {
112- throw new TypeError ( 'Promise resolved with itself' ) ;
119+ throw new TypeError ( TYPE_ERROR ) ;
113120 }
114121 if ( ( promise as any ) [ symbolState ] === UNRESOLVED ) {
115122 // should only get value.then once based on promise spec.
116123 let then : any = null ;
117124 try {
118- if ( typeof value === 'object' || typeof value === 'function' ) {
125+ if ( typeof value === OBJECT || typeof value === FUNCTION ) {
119126 then = value && value . then ;
120127 }
121128 } catch ( err ) {
@@ -130,7 +137,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
130137 ( value as any ) [ symbolState ] !== UNRESOLVED ) {
131138 clearRejectedNoCatch ( < Promise < any > > value ) ;
132139 resolvePromise ( promise , ( value as any ) [ symbolState ] , ( value as any ) [ symbolValue ] ) ;
133- } else if ( state !== REJECTED && typeof then === 'function' ) {
140+ } else if ( state !== REJECTED && typeof then === FUNCTION ) {
134141 try {
135142 then . apply ( value , [
136143 onceWrapper ( makeResolver ( promise , state ) ) , onceWrapper ( makeResolver ( promise , false ) )
@@ -148,7 +155,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
148155 // record task information in value when error occurs, so we can
149156 // do some additional work such as render longStackTrace
150157 if ( state === REJECTED && value instanceof Error ) {
151- ( value as any ) [ __symbol__ ( 'currentTask' ) ] = Zone . currentTask ;
158+ ( value as any ) [ CURRENT_TASK_SYMBOL ] = Zone . currentTask ;
152159 }
153160
154161 for ( let i = 0 ; i < queue . length ; ) {
@@ -176,6 +183,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
176183 return promise ;
177184 }
178185
186+ const REJECTION_HANDLED_HANDLER = __symbol__ ( 'rejectionHandledHandler' ) ;
179187 function clearRejectedNoCatch ( promise : ZoneAwarePromise < any > ) : void {
180188 if ( ( promise as any ) [ symbolState ] === REJECTED_NO_CATCH ) {
181189 // if the promise is rejected no catch status
@@ -184,8 +192,8 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
184192 // windows.rejectionhandled eventHandler or nodejs rejectionHandled
185193 // eventHandler
186194 try {
187- const handler = ( Zone as any ) [ __symbol__ ( 'rejectionHandledHandler' ) ] ;
188- if ( handler && typeof handler === 'function' ) {
195+ const handler = ( Zone as any ) [ REJECTION_HANDLED_HANDLER ] ;
196+ if ( handler && typeof handler === FUNCTION ) {
189197 handler . apply ( this , [ { rejection : ( promise as any ) [ symbolValue ] , promise : promise } ] ) ;
190198 }
191199 } catch ( err ) {
@@ -204,8 +212,8 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
204212 onFulfilled ?: ( value : R ) => U , onRejected ?: ( error : any ) => U ) : void {
205213 clearRejectedNoCatch ( promise ) ;
206214 const delegate = ( promise as any ) [ symbolState ] ?
207- ( typeof onFulfilled === 'function' ) ? onFulfilled : forwardResolution :
208- ( typeof onRejected === 'function' ) ? onRejected : forwardRejection ;
215+ ( typeof onFulfilled === FUNCTION ) ? onFulfilled : forwardResolution :
216+ ( typeof onRejected === FUNCTION ) ? onRejected : forwardRejection ;
209217 zone . scheduleMicroTask ( source , ( ) => {
210218 try {
211219 resolvePromise (
@@ -216,9 +224,11 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
216224 } ) ;
217225 }
218226
227+ const ZONE_AWARE_PROMISE_TO_STRING = 'function ZoneAwarePromise() { [native code] }' ;
228+
219229 class ZoneAwarePromise < R > implements Promise < R > {
220230 static toString ( ) {
221- return 'function ZoneAwarePromise() { [native code] }' ;
231+ return ZONE_AWARE_PROMISE_TO_STRING ;
222232 }
223233
224234 static resolve < R > ( value : R ) : Promise < R > {
@@ -364,7 +374,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
364374 patchThen ( NativePromise ) ;
365375
366376 let fetch = global [ 'fetch' ] ;
367- if ( typeof fetch == 'function' ) {
377+ if ( typeof fetch == FUNCTION ) {
368378 global [ 'fetch' ] = zoneify ( fetch ) ;
369379 }
370380 }
0 commit comments