async constructor

Because a constructor returns an object, we cannot declare it as async because an async function is inherently a promise. To await a promise inside a constructor, use an immediately invoked async function.


class MyClass {
  constructor() {
    return (async () => {
      this.value = await asyncFunction();
      return this;
    })();
  }
}