@@ -33,10 +33,16 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
3333 const rejection = e && e . rejection ;
3434 if ( rejection ) {
3535 console . error (
36- 'Unhandled Promise rejection:' ,
37- rejection instanceof Error ? rejection . message : rejection , '; Zone:' ,
38- ( < Zone > e . zone ) . name , '; Task:' , e . task && ( < Task > e . task ) . source , '; Value:' , rejection ,
39- rejection instanceof Error ? rejection . stack : undefined ) ;
36+ 'Unhandled Promise rejection:' ,
37+ rejection instanceof Error ? rejection . message : rejection ,
38+ '; Zone:' ,
39+ ( < Zone > e . zone ) . name ,
40+ '; Task:' ,
41+ e . task && ( < Task > e . task ) . source ,
42+ '; Value:' ,
43+ rejection ,
44+ rejection instanceof Error ? rejection . stack : undefined
45+ ) ;
4046 } else {
4147 console . error ( e ) ;
4248 }
@@ -67,8 +73,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
6773 if ( handler && typeof handler === 'function' ) {
6874 handler . call ( this , e ) ;
6975 }
70- } catch ( err ) {
71- }
76+ } catch ( err ) { }
7277 }
7378
7479 function isThenable ( value : any ) : boolean {
@@ -95,7 +100,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
95100 const REJECTED_NO_CATCH = 0 ;
96101
97102 function makeResolver ( promise : ZoneAwarePromise < any > , state : boolean ) : ( value : any ) => void {
98- return ( v ) => {
103+ return v => {
99104 try {
100105 resolvePromise ( promise , state , v ) ;
101106 } catch ( err ) {
@@ -124,7 +129,10 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
124129
125130 // Promise Resolution
126131 function resolvePromise (
127- promise : ZoneAwarePromise < any > , state : boolean , value : any ) : ZoneAwarePromise < any > {
132+ promise : ZoneAwarePromise < any > ,
133+ state : boolean ,
134+ value : any
135+ ) : ZoneAwarePromise < any > {
128136 const onceWrapper = once ( ) ;
129137 if ( promise === value ) {
130138 throw new TypeError ( TYPE_ERROR ) ;
@@ -143,16 +151,22 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
143151 return promise ;
144152 }
145153 // if (value instanceof ZoneAwarePromise) {
146- if ( state !== REJECTED && value instanceof ZoneAwarePromise &&
147- value . hasOwnProperty ( symbolState ) && value . hasOwnProperty ( symbolValue ) &&
148- ( value as any ) [ symbolState ] !== UNRESOLVED ) {
154+ if (
155+ state !== REJECTED &&
156+ value instanceof ZoneAwarePromise &&
157+ value . hasOwnProperty ( symbolState ) &&
158+ value . hasOwnProperty ( symbolValue ) &&
159+ ( value as any ) [ symbolState ] !== UNRESOLVED
160+ ) {
149161 clearRejectedNoCatch ( < Promise < any > > value ) ;
150162 resolvePromise ( promise , ( value as any ) [ symbolState ] , ( value as any ) [ symbolValue ] ) ;
151163 } else if ( state !== REJECTED && typeof then === 'function' ) {
152164 try {
153165 then . call (
154- value , onceWrapper ( makeResolver ( promise , state ) ) ,
155- onceWrapper ( makeResolver ( promise , false ) ) ) ;
166+ value ,
167+ onceWrapper ( makeResolver ( promise , state ) ) ,
168+ onceWrapper ( makeResolver ( promise , false ) )
169+ ) ;
156170 } catch ( err ) {
157171 onceWrapper ( ( ) => {
158172 resolvePromise ( promise , false , err ) ;
@@ -164,7 +178,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
164178 ( promise as any ) [ symbolValue ] = value ;
165179
166180 if ( ( promise as any ) [ symbolFinally ] === symbolFinally ) {
167- // the promise is generated by Promise.prototype.finally
181+ // the promise is generated by Promise.prototype.finally
168182 if ( state === RESOLVED ) {
169183 // the state is resolved, should ignore the value
170184 // and use parent promise value
@@ -177,34 +191,41 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
177191 // do some additional work such as render longStackTrace
178192 if ( state === REJECTED && value instanceof Error ) {
179193 // check if longStackTraceZone is here
180- const trace = Zone . currentTask && Zone . currentTask . data &&
181- ( Zone . currentTask . data as any ) [ creationTrace ] ;
194+ const trace =
195+ Zone . currentTask &&
196+ Zone . currentTask . data &&
197+ ( Zone . currentTask . data as any ) [ creationTrace ] ;
182198 if ( trace ) {
183199 // only keep the long stack trace into error when in longStackTraceZone
184- ObjectDefineProperty (
185- value , CURRENT_TASK_TRACE_SYMBOL ,
186- { configurable : true , enumerable : false , writable : true , value : trace } ) ;
200+ ObjectDefineProperty ( value , CURRENT_TASK_TRACE_SYMBOL , {
201+ configurable : true ,
202+ enumerable : false ,
203+ writable : true ,
204+ value : trace
205+ } ) ;
187206 }
188207 }
189208
190- for ( let i = 0 ; i < queue . length ; ) {
209+ for ( let i = 0 ; i < queue . length ; ) {
191210 scheduleResolveOrReject ( promise , queue [ i ++ ] , queue [ i ++ ] , queue [ i ++ ] , queue [ i ++ ] ) ;
192211 }
193212 if ( queue . length == 0 && state == REJECTED ) {
194213 ( promise as any ) [ symbolState ] = REJECTED_NO_CATCH ;
195214 try {
196215 // try to print more readable error log
197216 throw new Error (
198- 'Uncaught (in promise): ' + readableObjectToString ( value ) +
199- ( value && value . stack ? '\n' + value . stack : '' ) ) ;
217+ 'Uncaught (in promise): ' +
218+ readableObjectToString ( value ) +
219+ ( value && value . stack ? '\n' + value . stack : '' )
220+ ) ;
200221 } catch ( err ) {
201222 const error : UncaughtPromiseError = err ;
202223 error . rejection = value ;
203224 error . promise = promise ;
204225 error . zone = Zone . current ;
205226 error . task = Zone . currentTask ;
206227 _uncaughtPromiseErrors . push ( error ) ;
207- api . scheduleMicroTask ( ) ; // to make sure that it is running
228+ api . scheduleMicroTask ( ) ; // to make sure that it is running
208229 }
209230 }
210231 }
@@ -224,10 +245,9 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
224245 try {
225246 const handler = ( Zone as any ) [ REJECTION_HANDLED_HANDLER ] ;
226247 if ( handler && typeof handler === 'function' ) {
227- handler . call ( this , { rejection : ( promise as any ) [ symbolValue ] , promise : promise } ) ;
248+ handler . call ( this , { rejection : ( promise as any ) [ symbolValue ] , promise : promise } ) ;
228249 }
229- } catch ( err ) {
230- }
250+ } catch ( err ) { }
231251 ( promise as any ) [ symbolState ] = REJECTED ;
232252 for ( let i = 0 ; i < _uncaughtPromiseErrors . length ; i ++ ) {
233253 if ( promise === _uncaughtPromiseErrors [ i ] . promise ) {
@@ -238,30 +258,49 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
238258 }
239259
240260 function scheduleResolveOrReject < R , U1 , U2 > (
241- promise : ZoneAwarePromise < any > , zone : AmbientZone , chainPromise : ZoneAwarePromise < any > ,
242- onFulfilled ?: ( value : R ) => U1 , onRejected ?: ( error : any ) => U2 ) : void {
261+ promise : ZoneAwarePromise < any > ,
262+ zone : AmbientZone ,
263+ chainPromise : ZoneAwarePromise < any > ,
264+ onFulfilled ?: ( value : R ) => U1 ,
265+ onRejected ?: ( error : any ) => U2
266+ ) : void {
243267 clearRejectedNoCatch ( promise ) ;
244268 const promiseState = ( promise as any ) [ symbolState ] ;
245- const delegate = promiseState ?
246- ( typeof onFulfilled === 'function' ) ? onFulfilled : forwardResolution :
247- ( typeof onRejected === 'function' ) ? onRejected : forwardRejection ;
248- zone . scheduleMicroTask ( source , ( ) => {
249- try {
250- const parentPromiseValue = ( promise as any ) [ symbolValue ] ;
251- const isFinallyPromise = chainPromise && symbolFinally === ( chainPromise as any ) [ symbolFinally ] ;
252- if ( isFinallyPromise ) {
253- // if the promise is generated from finally call, keep parent promise's state and value
254- ( chainPromise as any ) [ symbolParentPromiseValue ] = parentPromiseValue ;
255- ( chainPromise as any ) [ symbolParentPromiseState ] = promiseState ;
269+ const delegate = promiseState
270+ ? typeof onFulfilled === 'function'
271+ ? onFulfilled
272+ : forwardResolution
273+ : typeof onRejected === 'function'
274+ ? onRejected
275+ : forwardRejection ;
276+ zone . scheduleMicroTask (
277+ source ,
278+ ( ) => {
279+ try {
280+ const parentPromiseValue = ( promise as any ) [ symbolValue ] ;
281+ const isFinallyPromise =
282+ chainPromise && symbolFinally === ( chainPromise as any ) [ symbolFinally ] ;
283+ if ( isFinallyPromise ) {
284+ // if the promise is generated from finally call, keep parent promise's state and value
285+ ( chainPromise as any ) [ symbolParentPromiseValue ] = parentPromiseValue ;
286+ ( chainPromise as any ) [ symbolParentPromiseState ] = promiseState ;
287+ }
288+ // should not pass value to finally callback
289+ const value = zone . run (
290+ delegate ,
291+ undefined ,
292+ isFinallyPromise && delegate !== forwardRejection && delegate !== forwardResolution
293+ ? [ ]
294+ : [ parentPromiseValue ]
295+ ) ;
296+ resolvePromise ( chainPromise , true , value ) ;
297+ } catch ( error ) {
298+ // if error occurs, should always return this error
299+ resolvePromise ( chainPromise , false , error ) ;
256300 }
257- // should not pass value to finally callback
258- const value = zone . run ( delegate , undefined , isFinallyPromise && delegate !== forwardRejection && delegate !== forwardResolution ? [ ] : [ parentPromiseValue ] ) ;
259- resolvePromise ( chainPromise , true , value ) ;
260- } catch ( error ) {
261- // if error occurs, should always return this error
262- resolvePromise ( chainPromise , false , error ) ;
263- }
264- } , chainPromise as TaskData ) ;
301+ } ,
302+ chainPromise as TaskData
303+ ) ;
265304 }
266305
267306 const ZONE_AWARE_PROMISE_TO_STRING = 'function ZoneAwarePromise() { [native code] }' ;
@@ -316,29 +355,33 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
316355 value = this . resolve ( value ) ;
317356 }
318357 value . then (
319- ( ( index ) => ( value : any ) => {
320- resolvedValues [ index ] = value ;
321- count -- ;
322- if ( ! count ) {
323- resolve ( resolvedValues ) ;
324- }
325- } ) ( count ) ,
326- reject ) ;
358+ ( index => ( value : any ) => {
359+ resolvedValues [ index ] = value ;
360+ count -- ;
361+ if ( ! count ) {
362+ resolve ( resolvedValues ) ;
363+ }
364+ } ) ( count ) ,
365+ reject
366+ ) ;
327367 count ++ ;
328368 }
329369 if ( ! count ) resolve ( resolvedValues ) ;
330370 return promise ;
331371 }
332372
333373 constructor (
334- executor :
335- ( resolve : ( value ?: R | PromiseLike < R > ) => void , reject : ( error ?: any ) => void ) => void ) {
374+ executor : (
375+ resolve : ( value ?: R | PromiseLike < R > ) => void ,
376+ reject : ( error ?: any ) => void
377+ ) => void
378+ ) {
336379 const promise : ZoneAwarePromise < R > = this ;
337380 if ( ! ( promise instanceof ZoneAwarePromise ) ) {
338381 throw new Error ( 'Must be an instanceof Promise.' ) ;
339382 }
340383 ( promise as any ) [ symbolState ] = UNRESOLVED ;
341- ( promise as any ) [ symbolValue ] = [ ] ; // queue;
384+ ( promise as any ) [ symbolValue ] = [ ] ; // queue;
342385 try {
343386 executor && executor ( makeResolver ( promise , RESOLVED ) , makeResolver ( promise , REJECTED ) ) ;
344387 } catch ( error ) {
@@ -347,11 +390,11 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
347390 }
348391
349392 then < TResult1 = R , TResult2 = never > (
350- onFulfilled ?: ( ( value : R ) => TResult1 | PromiseLike < TResult1 > ) | undefined | null ,
351- onRejected ?: ( ( reason : any ) => TResult2 | PromiseLike < TResult2 > ) | undefined |
352- null ) : Promise < TResult1 | TResult2 > {
353- const chainPromise : Promise < TResult1 | TResult2 > =
354- new ( this . constructor as typeof ZoneAwarePromise ) ( null ) ;
393+ onFulfilled ?: ( ( value : R ) => TResult1 | PromiseLike < TResult1 > ) | undefined | null ,
394+ onRejected ?: ( ( reason : any ) => TResult2 | PromiseLike < TResult2 > ) | undefined | null
395+ ) : Promise < TResult1 | TResult2 > {
396+ const chainPromise : Promise < TResult1 | TResult2 > = new ( this
397+ . constructor as typeof ZoneAwarePromise ) ( null ) ;
355398 const zone = Zone . current ;
356399 if ( ( this as any ) [ symbolState ] == UNRESOLVED ) {
357400 ( < any [ ] > ( this as any ) [ symbolValue ] ) . push ( zone , chainPromise , onFulfilled , onRejected ) ;
@@ -361,14 +404,16 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
361404 return chainPromise ;
362405 }
363406
364- catch < TResult = never > ( onRejected ?: ( ( reason : any ) => TResult | PromiseLike < TResult > ) | undefined |
365- null ) : Promise < R | TResult > {
407+ catch < TResult = never > (
408+ onRejected ?: ( ( reason : any ) => TResult | PromiseLike < TResult > ) | undefined | null
409+ ) : Promise < R | TResult > {
366410 return this . then ( null , onRejected ) ;
367411 }
368412
369413 finally < U > ( onFinally ?: ( ) => U | PromiseLike < U > ) : Promise < R > {
370- const chainPromise : Promise < R | never > =
371- new ( this . constructor as typeof ZoneAwarePromise ) ( null ) ;
414+ const chainPromise : Promise < R | never > = new ( this . constructor as typeof ZoneAwarePromise ) (
415+ null
416+ ) ;
372417 ( chainPromise as any ) [ symbolFinally ] = symbolFinally ;
373418 const zone = Zone . current ;
374419 if ( ( this as any ) [ symbolState ] == UNRESOLVED ) {
@@ -386,15 +431,15 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
386431 ZoneAwarePromise [ 'race' ] = ZoneAwarePromise . race ;
387432 ZoneAwarePromise [ 'all' ] = ZoneAwarePromise . all ;
388433
389- const NativePromise = global [ symbolPromise ] = global [ 'Promise' ] ;
434+ const NativePromise = ( global [ symbolPromise ] = global [ 'Promise' ] ) ;
390435 const ZONE_AWARE_PROMISE = Zone . __symbol__ ( 'ZoneAwarePromise' ) ;
391436
392437 let desc = ObjectGetOwnPropertyDescriptor ( global , 'Promise' ) ;
393438 if ( ! desc || desc . configurable ) {
394439 desc && delete desc . writable ;
395440 desc && delete desc . value ;
396441 if ( ! desc ) {
397- desc = { configurable : true , enumerable : true } ;
442+ desc = { configurable : true , enumerable : true } ;
398443 }
399444 desc . get = function ( ) {
400445 // if we already set ZoneAwarePromise, use patched one
@@ -477,5 +522,4 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
477522
478523 // This is not part of public API, but it is useful for tests, so we expose it.
479524 ( Promise as any ) [ Zone . __symbol__ ( 'uncaughtPromiseErrors' ) ] = _uncaughtPromiseErrors ;
480- return ZoneAwarePromise ;
481525} ) ;
0 commit comments