Async Class

Isiah Meadows isiahmeadows at gmail.com
Fri Feb 16 08:17:29 UTC 2018


 > I think he means literally returning a promise.

That would be the correct assumption. ;-)
-----

Isiah Meadows
me at isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Fri, Feb 16, 2018 at 3:05 AM, T.J. Crowder
<tj.crowder at farsightsoftware.com> wrote:
> 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/
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list