On Fri, Jul 20, 2018 at 12:15 PM Augusto Moura <[email protected]>
wrote:
> Interesting enough, I got a really weird case (reads contraintuitive, I'm
> pretty sure the semantics of the error are right) extending the Promise
> class to exemplify a simple Deferred implementation, the code:
>
> ``` js
> class Deferred extends Promise {
> constructor(factory) {
> super((resolve, reject) => {
> Object.assign(this, { reject, resolve });
> factory(resolve, reject);
> });
> }
> }
>
> const d = new Deferred(() => {});
> ```
> The problem is the usage of `this` before calling the super constructor
> (even when the using in the super call itself).
>
Isn't the solution the pattern we've already seen?
```js
class Deferred extends Promise {
constructor(factory) {
const methods = {};
super((resolve, reject) => Object.assign(methods, { resolve, reject }));
Object.assign(this, methods);
factory(this.resolve, this.reject);
}
}
```
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss