fix: should also allow subclass Promise without Symbol.species (#34533)

PR Close #34533
This commit is contained in:
JiaLiPassion 2019-12-22 13:15:08 +09:00 committed by Misko Hevery
parent 60018d265f
commit 58b29f1503
2 changed files with 10 additions and 3 deletions

View File

@ -384,7 +384,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
null): Promise<TResult1|TResult2> {
let C = (this.constructor as any)[Symbol.species];
if (!C || typeof C !== 'function') {
C = ZoneAwarePromise;
C = this.constructor || ZoneAwarePromise;
}
const chainPromise: Promise<TResult1|TResult2> = new (C as typeof ZoneAwarePromise)(noop);
const zone = Zone.current;

View File

@ -103,7 +103,14 @@ describe(
}).toThrowError('Must be an instanceof Promise.');
});
it('should allow subclassing with Promise.specices', () => {
it('should allow subclassing without Symbol.species', () => {
class MyPromise extends Promise<any> {
constructor(fn: any) { super(fn); }
}
expect(new MyPromise(() => {}).then(() => null) instanceof MyPromise).toBe(true);
});
it('should allow subclassing with Symbol.species', () => {
class MyPromise extends Promise<any> {
constructor(fn: any) { super(fn); }
@ -112,7 +119,7 @@ describe(
expect(new MyPromise(() => {}).then(() => null) instanceof MyPromise).toBe(true);
});
it('Promise.specices should return ZoneAwarePromise', () => {
it('Symbol.species should return ZoneAwarePromise', () => {
const empty = function() {};
const promise = Promise.resolve(1);
const FakePromise = ((promise.constructor = {} as any) as any)[Symbol.species] = function(