Skip to content

Commit e608e6c

Browse files
crisbetoalxhub
authored andcommitted
fix(zone.js): more robust check for promise-like objects (#57388)
Fixes that Zone.js wasn't checking properly if an object is promise-like. Fixes #57385. PR Close #57388
1 parent 57202c4 commit e608e6c

2 files changed

Lines changed: 10 additions & 16 deletions

File tree

packages/zone.js/lib/common/promise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function patchPromise(Zone: ZoneType): void {
8383
}
8484

8585
function isThenable(value: any): boolean {
86-
return value && value.then;
86+
return value && typeof value.then === 'function';
8787
}
8888

8989
function forwardResolution(value: any): any {

packages/zone.js/test/common/Promise.spec.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@ describe(
501501
(Promise as any)
502502
.race([Promise.reject('rejection1'), 'v1'])
503503
['catch']((v: any) => (value = v));
504-
// expect(Zone.current.get('queue').length).toEqual(2);
505504
flushMicrotasks();
506505
expect(value).toEqual('rejection1');
507506
});
@@ -513,7 +512,6 @@ describe(
513512
(Promise as any)
514513
.race([Promise.resolve('resolution'), 'v1'])
515514
.then((v: any) => (value = v));
516-
// expect(Zone.current.get('queue').length).toEqual(2);
517515
flushMicrotasks();
518516
expect(value).toEqual('resolution');
519517
});
@@ -525,22 +523,11 @@ describe(
525523
queueZone.run(() => {
526524
let value: any = null;
527525
Promise.all([Promise.reject('rejection'), 'v1'])['catch']((v: any) => (value = v));
528-
// expect(Zone.current.get('queue').length).toEqual(2);
529526
flushMicrotasks();
530527
expect(value).toEqual('rejection');
531528
});
532529
});
533530

534-
it('should resolve the value', () => {
535-
queueZone.run(() => {
536-
let value: any = null;
537-
Promise.all([Promise.resolve('resolution'), 'v1']).then((v: any) => (value = v));
538-
// expect(Zone.current.get('queue').length).toEqual(2);
539-
flushMicrotasks();
540-
expect(value).toEqual(['resolution', 'v1']);
541-
});
542-
});
543-
544531
it('should resolve with the sync then operation', () => {
545532
queueZone.run(() => {
546533
let value: any = null;
@@ -555,7 +542,6 @@ describe(
555542
},
556543
};
557544
Promise.all([p1, 'v1', p2]).then((v: any) => (value = v));
558-
// expect(Zone.current.get('queue').length).toEqual(2);
559545
flushMicrotasks();
560546
expect(value).toEqual(['p1', 'v1', 'p2']);
561547
});
@@ -578,13 +564,21 @@ describe(
578564
Promise.all(generators()).then((val) => {
579565
value = val;
580566
});
581-
// expect(Zone.current.get('queue').length).toEqual(2);
582567
flushMicrotasks();
583568
expect(value).toEqual([1, 2]);
584569
});
585570
},
586571
),
587572
);
573+
574+
it('should handle object with a truthy `then property`', () => {
575+
queueZone.run(() => {
576+
let value: any = null;
577+
Promise.all([{then: 123}]).then((v: any) => (value = v));
578+
flushMicrotasks();
579+
expect(value).toEqual([jasmine.objectContaining({then: 123})]);
580+
});
581+
});
588582
});
589583
});
590584

0 commit comments

Comments
 (0)