Async Class

T.J. Crowder tj.crowder at farsightsoftware.com
Fri Feb 16 08:05:01 UTC 2018


On Fri, Feb 16, 2018 at 7:51 AM, Dimitrian Nine <dimtimeforever at gmail.com>
wrote:
>
> "Also, note that you can return promises out of the constructor (and I've
done it before)"
>
> Like callback?

I think he means literally returning a promise. If you return an object out
of the constructor, that becomes the result of the `new` operation. So you
can literally return a promise of an instance instead of an instance
([fiddle][1]):

```js
class Example {
    constructor() {
        return new Promise(resolve => {
            setTimeout(() => {
                const instance = Object.create(Example.prototype);
                resolve(instance);
            }, 200);
        });
    }

    foo() {
        console.log("foo called");
    }
}
```

It gets ugly(ier) when you want to extend that. :-)

-- T.J. Crowder

[1]: https://jsfiddle.net/b9kzpd6c/1/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180216/86dade56/attachment.html>


More information about the es-discuss mailing list