Promise capability support
Richard Gibson
richard.gibson at gmail.com
Fri Jul 20 18:56:57 UTC 2018
On Fri, Jul 20, 2018 at 12:15 PM Augusto Moura <augusto.borgesm at gmail.com>
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);
}
}
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180720/89f0c204/attachment.html>
More information about the es-discuss
mailing list