Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

support real subclass + constructor tampering #172

Open
stefanpenner opened this issue Feb 29, 2016 · 1 comment
Open

support real subclass + constructor tampering #172

stefanpenner opened this issue Feb 29, 2016 · 1 comment

Comments

@stefanpenner
Copy link
Owner

this is relatively high priority, but will require some aggressive refactoring to do performently.

@eKoopmans
Copy link

Hi @stefanpenner, thanks for the library, it's great!

Looks like you're aware of subclassing problems, just wanted to share mine. I've run into a problem with subclassing and es6-promise - specifically in then, with const child = new this.constructor(noop); This behaviour seems contrary to native implementations (at least in Chrome and Firefox), which always create a new Promise, regardless of the class of this.

Keeping the child as a Promise lets us reuse/extend Promise.then within our subclass, like so (see working example here):

PromiseSubclass.prototype.then = function then(resolve, reject) {
  var returnVal = Promise.prototype.then.call(this, resolve, reject);
  // Other actions...
  return PromiseSubclass.convert(returnVal);
};

However, using this.constructor ends up creating a recursive definition of then that never resolves.

I have built in a work-around in my subclass by detecting if it's an es6-promise (by checking for _state) and cloning into a new Promise before sending to then, so that it accesses the right constructor. So it's all good on my end, just wanted to raise that flag in case it helps!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants