5

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.

ConductedClever
  • 4,175
  • 2
  • 35
  • 69
  • 1
    I had a similar requirement on a web app and to solve it I just load the server data on the index page before I load my aurelia app and put it on the global scope so I can access it easily from the aurelia app, like `window.ServerMetadata = {};` – Leo Mar 18 '19 at 17:13
  • 1
    Possible duplicate of [calling an async function in the constructor.](https://stackoverflow.com/questions/49694779/calling-an-async-function-in-the-constructor) and [Asynchronous data loading in class constructor](https://stackoverflow.com/questions/37351146) and [Does async/await will allow us to be used on constructors?](https://stackoverflow.com/questions/36363278) and [Async/Await Class Constructor](https://stackoverflow.com/questions/43431550) – adiga Mar 18 '19 at 19:10
  • [Don't do asynchronous http requests in your constructor.](https://stackoverflow.com/q/24398699/1048572) Do them *before* constructing the instance. – Bergi Mar 18 '19 at 19:20
  • @Leo of course, this will be a possible solution. I will do it if no better one found. Thanks. – ConductedClever Mar 25 '19 at 05:37
  • @adiga, I think you have not read the question. The questions you have mentioned, are the places where we can wait for the on the outer side. Please read the UPDATE part for more explanation. – ConductedClever Mar 25 '19 at 05:39
  • @Bergi in general places this hint will be accepted, but in aureliajs or other SPA frameworks, I am not constructing the components. So the question is absolutely what you skipped: how to tell aureliajs to not construct the components until async function resolved. – ConductedClever Mar 25 '19 at 05:42

1 Answers1

2

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();
}
bigopon
  • 1,924
  • 2
  • 14
  • 23
  • Thanks bigopon. Excellent review on possible solutions. And what about this one: in app.js not configing router (and also showing a loading indicator) and after async jobs done, add the routes to router. – ConductedClever Mar 25 '19 at 09:54
  • Probably an error somewhere during bootstrapping. Simply wrap everything inside configure fn with try/catch and log it to debug further i suppose – bigopon Mar 26 '19 at 10:12