@@ -33,10 +33,16 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
33
33
const rejection = e && e . rejection ;
34
34
if ( rejection ) {
35
35
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
+ ) ;
40
46
} else {
41
47
console . error ( e ) ;
42
48
}
@@ -67,8 +73,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
67
73
if ( handler && typeof handler === 'function' ) {
68
74
handler . call ( this , e ) ;
69
75
}
70
- } catch ( err ) {
71
- }
76
+ } catch ( err ) { }
72
77
}
73
78
74
79
function isThenable ( value : any ) : boolean {
@@ -95,7 +100,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
95
100
const REJECTED_NO_CATCH = 0 ;
96
101
97
102
function makeResolver ( promise : ZoneAwarePromise < any > , state : boolean ) : ( value : any ) => void {
98
- return ( v ) => {
103
+ return v => {
99
104
try {
100
105
resolvePromise ( promise , state , v ) ;
101
106
} catch ( err ) {
@@ -124,7 +129,10 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
124
129
125
130
// Promise Resolution
126
131
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 > {
128
136
const onceWrapper = once ( ) ;
129
137
if ( promise === value ) {
130
138
throw new TypeError ( TYPE_ERROR ) ;
@@ -143,16 +151,22 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
143
151
return promise ;
144
152
}
145
153
// 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
+ ) {
149
161
clearRejectedNoCatch ( < Promise < any > > value ) ;
150
162
resolvePromise ( promise , ( value as any ) [ symbolState ] , ( value as any ) [ symbolValue ] ) ;
151
163
} else if ( state !== REJECTED && typeof then === 'function' ) {
152
164
try {
153
165
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
+ ) ;
156
170
} catch ( err ) {
157
171
onceWrapper ( ( ) => {
158
172
resolvePromise ( promise , false , err ) ;
@@ -164,7 +178,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
164
178
( promise as any ) [ symbolValue ] = value ;
165
179
166
180
if ( ( promise as any ) [ symbolFinally ] === symbolFinally ) {
167
- // the promise is generated by Promise.prototype.finally
181
+ // the promise is generated by Promise.prototype.finally
168
182
if ( state === RESOLVED ) {
169
183
// the state is resolved, should ignore the value
170
184
// and use parent promise value
@@ -177,34 +191,41 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
177
191
// do some additional work such as render longStackTrace
178
192
if ( state === REJECTED && value instanceof Error ) {
179
193
// 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 ] ;
182
198
if ( trace ) {
183
199
// 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
+ } ) ;
187
206
}
188
207
}
189
208
190
- for ( let i = 0 ; i < queue . length ; ) {
209
+ for ( let i = 0 ; i < queue . length ; ) {
191
210
scheduleResolveOrReject ( promise , queue [ i ++ ] , queue [ i ++ ] , queue [ i ++ ] , queue [ i ++ ] ) ;
192
211
}
193
212
if ( queue . length == 0 && state == REJECTED ) {
194
213
( promise as any ) [ symbolState ] = REJECTED_NO_CATCH ;
195
214
try {
196
215
// try to print more readable error log
197
216
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
+ ) ;
200
221
} catch ( err ) {
201
222
const error : UncaughtPromiseError = err ;
202
223
error . rejection = value ;
203
224
error . promise = promise ;
204
225
error . zone = Zone . current ;
205
226
error . task = Zone . currentTask ;
206
227
_uncaughtPromiseErrors . push ( error ) ;
207
- api . scheduleMicroTask ( ) ; // to make sure that it is running
228
+ api . scheduleMicroTask ( ) ; // to make sure that it is running
208
229
}
209
230
}
210
231
}
@@ -224,10 +245,9 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
224
245
try {
225
246
const handler = ( Zone as any ) [ REJECTION_HANDLED_HANDLER ] ;
226
247
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 } ) ;
228
249
}
229
- } catch ( err ) {
230
- }
250
+ } catch ( err ) { }
231
251
( promise as any ) [ symbolState ] = REJECTED ;
232
252
for ( let i = 0 ; i < _uncaughtPromiseErrors . length ; i ++ ) {
233
253
if ( promise === _uncaughtPromiseErrors [ i ] . promise ) {
@@ -238,30 +258,49 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
238
258
}
239
259
240
260
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 {
243
267
clearRejectedNoCatch ( promise ) ;
244
268
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 ) ;
256
300
}
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
+ ) ;
265
304
}
266
305
267
306
const ZONE_AWARE_PROMISE_TO_STRING = 'function ZoneAwarePromise() { [native code] }' ;
@@ -316,29 +355,33 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
316
355
value = this . resolve ( value ) ;
317
356
}
318
357
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
+ ) ;
327
367
count ++ ;
328
368
}
329
369
if ( ! count ) resolve ( resolvedValues ) ;
330
370
return promise ;
331
371
}
332
372
333
373
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
+ ) {
336
379
const promise : ZoneAwarePromise < R > = this ;
337
380
if ( ! ( promise instanceof ZoneAwarePromise ) ) {
338
381
throw new Error ( 'Must be an instanceof Promise.' ) ;
339
382
}
340
383
( promise as any ) [ symbolState ] = UNRESOLVED ;
341
- ( promise as any ) [ symbolValue ] = [ ] ; // queue;
384
+ ( promise as any ) [ symbolValue ] = [ ] ; // queue;
342
385
try {
343
386
executor && executor ( makeResolver ( promise , RESOLVED ) , makeResolver ( promise , REJECTED ) ) ;
344
387
} catch ( error ) {
@@ -347,11 +390,11 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
347
390
}
348
391
349
392
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 ) ;
355
398
const zone = Zone . current ;
356
399
if ( ( this as any ) [ symbolState ] == UNRESOLVED ) {
357
400
( < any [ ] > ( this as any ) [ symbolValue ] ) . push ( zone , chainPromise , onFulfilled , onRejected ) ;
@@ -361,14 +404,16 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
361
404
return chainPromise ;
362
405
}
363
406
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 > {
366
410
return this . then ( null , onRejected ) ;
367
411
}
368
412
369
413
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
+ ) ;
372
417
( chainPromise as any ) [ symbolFinally ] = symbolFinally ;
373
418
const zone = Zone . current ;
374
419
if ( ( this as any ) [ symbolState ] == UNRESOLVED ) {
@@ -386,15 +431,15 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
386
431
ZoneAwarePromise [ 'race' ] = ZoneAwarePromise . race ;
387
432
ZoneAwarePromise [ 'all' ] = ZoneAwarePromise . all ;
388
433
389
- const NativePromise = global [ symbolPromise ] = global [ 'Promise' ] ;
434
+ const NativePromise = ( global [ symbolPromise ] = global [ 'Promise' ] ) ;
390
435
const ZONE_AWARE_PROMISE = Zone . __symbol__ ( 'ZoneAwarePromise' ) ;
391
436
392
437
let desc = ObjectGetOwnPropertyDescriptor ( global , 'Promise' ) ;
393
438
if ( ! desc || desc . configurable ) {
394
439
desc && delete desc . writable ;
395
440
desc && delete desc . value ;
396
441
if ( ! desc ) {
397
- desc = { configurable : true , enumerable : true } ;
442
+ desc = { configurable : true , enumerable : true } ;
398
443
}
399
444
desc . get = function ( ) {
400
445
// if we already set ZoneAwarePromise, use patched one
@@ -477,5 +522,4 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
477
522
478
523
// This is not part of public API, but it is useful for tests, so we expose it.
479
524
( Promise as any ) [ Zone . __symbol__ ( 'uncaughtPromiseErrors' ) ] = _uncaughtPromiseErrors ;
480
- return ZoneAwarePromise ;
481
525
} ) ;
0 commit comments