Async Class

Isiah Meadows isiahmeadows at gmail.com
Mon Aug 26 20:40:35 UTC 2019


Not a bad idea, but I'd *strongly* prefer the promise to be returned
from `new AsyncClass(...)` itself instead. And down that vein, `super`
with async classes should also implicitly `await` in its `super` call,
setting `this` to the value itself. For sanity reasons, `super` should
return a promise resolved with the set `this`, so you can still use it
and await it in inner arrow functions (it's possible), but it should
still do its own implicit awaiting, just so you can add properties to
the right instance. (This will also come into play with private
properties - you want those on the *instance*, not the *promise*.)

I do have a preference: `async` should decorate the `constructor`, not
the `class`. It's not the class that's async, it's the constructor
itself. You can still have sync methods elsewhere in the class, and
this doesn't do anything to affect that, hence why I'd prefer an
`async constructor(...) { ... }` instead of an `async class`.

-----

Isiah Meadows
contact at isiahmeadows.com
www.isiahmeadows.com

On Mon, Aug 26, 2019 at 11:11 AM Dimitrian Nine
<dimtimeforever at gmail.com> wrote:
>
> Ok so much time has passed. I have learned more js. And created some [wrapper][1] for my idea:
> Class is just function constructor that return object
> Async Class is async function constructor that returns promise object. Wrapper code:
> ```js
> class PromiseClass { //promisified class
> static async new(obj){ return obj;}
> constructor() {return PromiseClass.new(this); }//new
> }//class
> class AsyncClass extends PromiseClass{ // can ext Class|PromiseClass
> static async new(obj){return await obj();}
> constructor(){return AsyncClass.new(async()=>{await super(); return this});}//new
> }//AsyncClass
> ```
> And we work with Async Class like we work with functions. I dont see here some differents to use them. Code without wrapper be clean:
> ```js
> async class PromiseClass { //return Promise
> constructor() {}//async()=>Promise object
> }//class
> async class AsyncClass extends PromiseClass{ // can ext Class|PromiseClass
> constructor(){ await super();} //async()=>Promise object
> }//AsyncClass
> ```
> [1]:https://repl.it/repls/InsubstantialPortlyCores
> вс, 25 февр. 2018 г. в 11:47, Dimitrian Nine <dimtimeforever at gmail.com>:
>>
>> ([Classes][1]):
>> [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
>> «JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript»
>> «Classes are in fact "special functions"»
>>
>> ```js class = function Create(); ```
>> all i want:
>> ``` js async class = async function Create(); ```
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list