ConductedClever
ConductedClever

Reputation: 4295

Aureliajs Waiting For Data on App Constructor

I am developing an app in aureliajs. The development process is started for many months and now, the back-end developers want to make their services versioned. So I have a web service to call to get the version of each server side (web api) app and then, for the further requests, call the right api address including its version.

So, in the app.js I am requesting the system meta and storing it somewhere. But some components get initialized before this request gets done. So they won't find the version initialized and requesting the wrong server data.

I want to make the app.js constructor wait until this data is retrieved. For example something like this:

export class App {
  async constructor(...) {
    ...

    await this.initializeHttp();

    ...
  }

  initializeHttp(){
    // get the system meta from server
  }
}

but this solution is not applicable. Because the constructor can't be async. So how should I block the job until the system meta is retrieved?

UPDATE

The question is not a duplicate of this question. In that question, there is a place in outer class to await for the initialization job; although in my question, the main problem is, where to put this await-tion. So the question is not just about async function in constructor, but is about blocking all aurelia jobs until async job resolves.

Upvotes: 6

Views: 372

Answers (1)

bigopon
bigopon

Reputation: 1964

Aurelia provides many ways to handle asynchronous flow. If your custom element is a routed component, then you can leverage activate lifecycle to return a promise and initialize the http service asynchronously.

Otherwise, you can use CompositionTransaction to halt the process further, before you are done with initialization. You can see a preliminary example at https://tungphamblog.wordpress.com/2016/08/15/aurelia-customelement-async/

You can also leverage async nature of configure function in bootstrapping an Aurelia application to do initialization there:

export function configure(aurelia) {
  ...
  await aurelia.container.get(HttpServiceInitializer).initialize();
}

Upvotes: 3

Related Questions