You are right, I didn't know you can use variables before calling the super
constructor (a Java thing).
So yeah, it's pretty easy to extend a Promise to externalize resolve and
reject

---
PS: Sorry about my last email last paragraph grammar, I'm yet getting used
to write long texts in English

Em sex, 20 de jul de 2018 às 15:57, Richard Gibson <richard.gib...@gmail.com>
escreveu:

> On Fri, Jul 20, 2018 at 12:15 PM Augusto Moura <augusto.borg...@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);
>   }
> }
> ```
>
-- 
Augusto Moura
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to