Async Class

kai zhu kaizhu256 at gmail.com
Tue Aug 27 17:14:55 UTC 2019


class-factories are good enough. as real-world example, google-puppeteer
uses class-factories to promisify its constructors, which are
easy-to-reason.

if the below Browser/Page classes were also "async", it would be difficult
to
debug their intent in real-world code -- are their instances supposed to act
as promises or as data-objects? i imagine code-maintennance would be a
nightmare.



```js
// "await" puppeteer's Browser constructor
// https://github.com/GoogleChrome/puppeteer/blob/v1.19.0/lib/Browser.js#L23
class Browser extends EventEmitter {
    constructor(connection, contextIds, ...) {
        ...
    }
    static async create(connection, contextIds, ...) {
        const browser = new Browser(connection, contextIds, ...);
        await connection.send(...);
        return browser;
    }
}
//
https://github.com/GoogleChrome/puppeteer/blob/v1.19.0/lib/Launcher.js#L184
const browser = await Browser.create(connection, [], ...);



// "await" puppeteer's Page constructor
// https://github.com/GoogleChrome/puppeteer/blob/v1.19.0/lib/Page.js#L36
class Page extends EventEmitter {
    constructor(client, target, ...) {
        ...
    }
    static async create(client, target, ...) {
        const page = new Page(client, target, ...);
        await page._initialize();
        ...
        return page;
    }
}
//
https://github.com/kaizhu256/puppeteer-to-istanbul-example/blob/6b3f599f/screenshot.js#L317
page1 = await module.exports.Page.create(null, tmp);
```




On Tue, Aug 27, 2019 at 11:36 AM Claude Pache <claude.pache at gmail.com>
wrote:

>
>
> Le 26 août 2019 à 17:11, Dimitrian Nine <dimtimeforever at gmail.com> a
> écrit :
>
> Class is just function constructor that return object
>
>
> Although in JS a class is represented by the same object as its
> constructor, I don’t think it is good to assimilate the concept of JS class
> and JS constructor. Indeed, a class consists not only of its constructor,
> but also of all its associated methods. This is what is suggested by the
> syntax:
>
> ```js
> class C {
>     /* definition of all methods, not only constructor */
> }
> ```
>
> From that perspective, I understand very well what would be an “async
> constructor”; but an “async class” doesn’t make much sense.
>
> —Claude
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190827/797e72e1/attachment.html>


More information about the es-discuss mailing list