Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 2f11e67

Browse files
JiaLiPassionmhevery
authored andcommitted
feat(patch): fix #499, let promise instance toString active like native (#734)
1 parent a89830d commit 2f11e67

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

lib/browser/browser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {patchTimer} from '../common/timers';
10-
import {patchFuncToString} from '../common/to-string';
10+
import {patchFuncToString, patchObjectToString} from '../common/to-string';
1111
import {findEventTask, patchClass, patchEventTargetMethods, patchMethod, patchPrototype, zoneSymbol} from '../common/utils';
1212

1313
import {propertyPatch} from './define-property';
@@ -154,6 +154,8 @@ if (_global['navigator'] && _global['navigator'].geolocation) {
154154

155155
// patch Func.prototype.toString to let them look like native
156156
patchFuncToString();
157+
// patch Object.prototype.toString to let them look like native
158+
patchObjectToString();
157159

158160
// handle unhandled promise rejection
159161
function findPromiseRejectionHandler(evtName: string) {

lib/common/to-string.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ export function patchFuncToString() {
3434
return originalFunctionToString.apply(this, arguments);
3535
};
3636
}
37+
38+
export function patchObjectToString() {
39+
const originalObjectToString = Object.prototype.toString;
40+
Object.prototype.toString = function() {
41+
if (this instanceof Promise) {
42+
return '[object Promise]';
43+
}
44+
return originalObjectToString.apply(this, arguments);
45+
};
46+
}

lib/node/node.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import './events';
1111
import './fs';
1212

1313
import {patchTimer} from '../common/timers';
14-
import {patchFuncToString} from '../common/to-string';
14+
import {patchFuncToString, patchObjectToString} from '../common/to-string';
1515
import {findEventTask, patchMacroTask, patchMicroTask, zoneSymbol} from '../common/utils';
1616

1717
const set = 'set';
@@ -38,6 +38,8 @@ handleUnhandledPromiseRejection();
3838

3939
// patch Function.prototyp.toString
4040
patchFuncToString();
41+
// patch Object.prototyp.toString
42+
patchObjectToString();
4143

4244
// Crypto
4345
let crypto: any;

test/common/Promise.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ describe(
6060
expect(String(Promise).indexOf('[native code]') >= 0).toBe(true);
6161
});
6262

63+
it('should use native toString for promise instance', () => {
64+
expect(Object.prototype.toString.call(Promise.resolve())).toEqual('[object Promise]');
65+
});
66+
6367
it('should make sure that new Promise is instance of Promise', () => {
6468
expect(Promise.resolve(123) instanceof Promise).toBe(true);
6569
expect(new Promise(() => null) instanceof Promise).toBe(true);

0 commit comments

Comments
 (0)