Skip to content

Commit 18e474f

Browse files
riflermhevery
authored andcommitted
fix(zone.js): zone.js toString patch should check typeof Promise is function (#38350)
Close #38361 zone.js monkey patch toString, and check the instance is `Promise` or not by using `instanceof Promise`, sometimes when Promise is not available, the `instanceof` operation fails and throw `TypeError: Right-hand side of 'instanceof' is not an object` this PR check `typeof Promise` equals to function or not to prevent the error. PR Close #38350
1 parent cb3db0d commit 18e474f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/zone.js/lib/common/to-string.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ Zone.__load_patch('toString', (global: any) => {
4949
const originalObjectToString = Object.prototype.toString;
5050
const PROMISE_OBJECT_TO_STRING = '[object Promise]';
5151
Object.prototype.toString = function() {
52-
if (this instanceof Promise) {
52+
if (typeof Promise === 'function' && this instanceof Promise) {
5353
return PROMISE_OBJECT_TO_STRING;
5454
}
55+
5556
return originalObjectToString.call(this);
5657
};
5758
});

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ describe('global function patch', () => {
1818
.toEqual(Function.prototype.toString.call(g[zoneSymbol('setTimeout')]));
1919
});
2020

21+
it('should not throw error if Promise is not a function', () => {
22+
const P = g.Promise;
23+
try {
24+
g.Promise = undefined;
25+
expect(() => {
26+
const a = {}.toString();
27+
}).not.toThrow();
28+
} finally {
29+
g.Promise = P;
30+
}
31+
});
32+
2133
it('MutationObserver toString should be the same with native version',
2234
ifEnvSupports('MutationObserver', () => {
2335
const nativeMutationObserver = g[zoneSymbol('MutationObserver')];

0 commit comments

Comments
 (0)