Async Class
kdex
kdex at kdex.de
Sun Feb 18 11:08:18 UTC 2018
Having the constructor return a `Promise` is generally considered bad
practice, since it breaks `new X instanceof X`, which in general breaks
inheritance.
So, for this idea to work, you really shouldn't mark `constructor` as `async`
(since this might suggest it returning a `Promise`, which it mustn't);
instead, you would have to make the constructor return `this`, then make the
asynchronous initialization, and then execute the rest. In other words,
something like this:
```js
async class X {
constructor() {
await this.read();
await this.write();
}
async read() { … }
async write() { … }
}
console.log(new X());
```
would transpile like so:
```js
class X {
constructor() {}
async initialize() {
await this.read();
await this.write();
}
async read() { … }
async write() { … }
}
(new X()).initialize().then(x => {
console.log(x);
});
```
The next problem with this idea is that you can't statically decide what `new
something` is about to construct. Since `something` might be an `async class`,
the StatementList following the construction of `something` would always have
to be wrapped in a callback.
Long story short, introducing this feature would slow down `new`/
`Reflect.construct`.
On Friday, February 16, 2018 5:12:10 AM CET Dimitrian Nine wrote:
> I cant find: was or not that proposal...
>
> But can we have Async Class with async constructor and async super?
>
> //AsyncClass
> async_class AsyncClass { // keyword async_class(example)
> constructor() {
> await something(); //now constructor async too
> }}
>
> //AsyncObj in some async method
> let NewAsyncObj = await AsyncClass(); // create async obj
>
> //Extends
> async_class ExtAsyncClass extends AsyncClass{
> constructor() {
> await super(); // now we can await super too
> await something();
> }}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180218/bce50237/attachment.sig>
More information about the es-discuss
mailing list