2

In my Ionic app, I have two async requests that must complete before I want my app component to load as part of a fairly involved initialization procedure. The first request is configuration related and the second is session related. They must complete in succession.

Simply chaining the two request observables is not an option because of dependencies (I won’t elaborate here). However I would assume that APP_INITIALIZER, being a multi-provider, would allow me to chain two together, each with their own injected dependencies.

Is it possible to have multiple APP_INITIALIZERs and if so, I’d love to see a real-world example. If not, is there a better way to do get the same result?

Nemgathos
  • 605
  • 1
  • 5
  • 13
snort
  • 2,285
  • 3
  • 19
  • 21
  • https://stackoverflow.com/questions/37611549/how-to-pass-parameters-rendered-from-backend-to-angular2-bootstrap-method/37611614#37611614 or https://stackoverflow.com/search?tab=votes&q=%5bangular%5d%20user%3a217408%20app_initializer – Günter Zöchbauer Nov 29 '17 at 18:06
  • @GünterZöchbauer, Thanks, but unless I've missed something that seems to address only a single async initializer, which I'm already familiar with. – snort Nov 30 '17 at 16:16

1 Answers1

2

If on app_initializer depends on the other, you can't just add two app_initializers. They'll be executed independently. You can inject a shared service that contains an observable that emits when the first app_initializer has completed, and the second initializer can subscribe to that observable and make his request when an event notifies it.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks. That makes sense. I was assuming the injector would figure out the correct sequence like it does with other providers, but I can't even figure out how to get both of them to be called regardless. – snort Nov 30 '17 at 21:26