Skip to content

CodeCoupler UI Async Contructor Pattern

This class is uesed internally. Normally you never have to use this.

Classes derived from here can initiated with

1
let myClass = new MyClass().initialized;

Or with:

1
2
let myClass = new Class();
await myClass.initialized;

Classes derived from here have to implement their asynchronous initialisation procedure within an method "init":

1
2
3
4
5
class MyClass extends AsyncInit {
  async init(...the arguments from constructor...) {
    //Do something
  }
}

You can implement an additional method async prepare if you need to do things before init is called. Depending on the return value the following action will be taken:

  • If you return here false, init will not be executed.
  • If you return something different from true the the initialized resolves to this returned value and init will not be executed. So a await x = new Something().initialized will result to this value as new instance.

Please keep in mind, that if you use a prepare method that could eventually return another instance you should instruct the users to reassign the variable if not using the oneliner:

1
2
let myClass = new Class();
myClass = await myClass.initialized;

The first line would just return a new instatiated class, but the second line would return the instance you have specified with the method "prepare".