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

Make the check for ZoneAwarePromise more stringent #495

Merged
merged 1 commit into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,10 @@ const Zone: ZoneType = (function(global: any) {
function resolvePromise(
promise: ZoneAwarePromise<any>, state: boolean, value: any): ZoneAwarePromise<any> {
if (promise[symbolState] === UNRESOLVED) {
if (value instanceof ZoneAwarePromise && value[symbolState] !== UNRESOLVED) {
if (value instanceof ZoneAwarePromise &&
value.hasOwnProperty(symbolState) &&
value.hasOwnProperty(symbolValue) &&
value[symbolState] !== UNRESOLVED) {
clearRejectedNoCatch(<Promise<any>>value);
resolvePromise(promise, value[symbolState], value[symbolValue]);
} else if (isThenable(value)) {
Expand Down
47 changes: 47 additions & 0 deletions test/common/Promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,53 @@ describe(
});
});

describe('Promise subclasses', function() {
function MyPromise(init) {
this._promise = new Promise(init);
}

MyPromise.prototype.catch = function _catch() {
return this._promise.catch.apply(this._promise, arguments);
};

MyPromise.prototype.then = function then() {
return this._promise.then.apply(this._promise, arguments);
};

var setPrototypeOf = (Object as any).setPrototypeOf || function(obj, proto) {
obj.__proto__ = proto;
return obj;
}

setPrototypeOf(MyPromise.prototype, Promise.prototype);

it('should reject if the Promise subclass rejects', function() {
var myPromise = new MyPromise(function(resolve, reject) {
reject('foo');
});

return Promise.resolve().then(function() {
return myPromise;
}).then(function() {
throw new Error('Unexpected resolution');
}, function(result) {
expect(result).toBe('foo');
});
});

it('should resolve if the Promise subclass resolves', function() {
var myPromise = new MyPromise(function(resolve, reject) {
resolve('foo');
});

return Promise.resolve().then(function() {
return myPromise;
}).then(function(result) {
expect(result).toBe('foo');
});
});
});

describe('fetch', ifEnvSupports('fetch', function() {
it('should work for text response', function(done) {
testZone.run(function() {
Expand Down