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

Zone.js issue with Promise "subclass" #494

@markandrus

Description

@markandrus

Hi,

I am having an issue using Zone.js alongside a Promise "subclass" (I say "subclass" because I am not quite sure my method of subclassing is correct). Assume the following code is run in a browser after loading Zone.js.

Promise Subclass

First, my Promise subclass:

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);
};

Object.setPrototypeOf(MyPromise.prototype, Promise.prototype);

This is enough to get an instance of MyPromise passing an instanceof check for Promise.

Test Scenario

Promise.resolve().then(function() {
  return new MyPromise(function(resolve) {
    resolve(':-)');
  });
}).then(function(result) {
  console.log('Success', result);
}, function(error) {
  console.error('Failure', error);                                              
});

Expected Result

Success :-)

Actual Result

Failure undefined

Analysis

After some digging, I think the check in resolvePromise should be more stringent. Since MyPromise inherits from Promise in the example above (which gets overwritten to ZoneAwarePromise), instances of MyPromise pass the ZoneAwarePromise instanceof check. However, instances of MyPromise do not define the __zone_symbol__state or __zone_symbol_value properties that are being checked for. The result is that resolvePromise is called again with the MyPromise instance, state undefined, and value undefined. Eventually, this leads to a call to scheduleResolveOrReject where the delegate gets set the onRejected handler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions