San Jay Falcon
San Jay Falcon

Reputation: 1013

APP_INITIALIZER blocks loading of the App on error

I'am catching the error in the Subscribe and the enclosing Promise.

    @Injectable()
    export class CountryListResolver {

        public countryList: Locale[];

        constructor(private portareServices: PortareServices) { }

        load() {
            return new Promise((resolve, reject) => {
                this.portareServices.getCountryList().subscribe((data) => {
                    this.countryList = data;
                    resolve(true);
                },
                error => {
                    console.log('CountryListResolver', error);
                },
                () => {
                    // No errors, route to new page
                });
            }).catch((err: any) => Promise.resolve());
        }
    }

Now the thing is, when I uncomment the functionality of actually getting the data from the service, the app just loads fine (obviously without having the data in place which should have neen loaded initially).

  // this.localesEU = this.portareDataModel.setLocalesEU = this.countryListResolver.countryList;

App loads just fine when commented out the load function. Also, if service resolves succesfully, everything just works like a charm.

  providers: [
      CountryListResolver, { provide: APP_INITIALIZER, useFactory: countryListProviderFactory, deps: [CountryListResolver], multi: true }
  ]

How to handle this issue?

Upvotes: 3

Views: 2588

Answers (1)

Coderer
Coderer

Reputation: 27294

Per this related answer, when APP_INITIALIZER rejects, it falls out of the promise returned by bootstrapModule:

// main.ts

platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => { ... fail gracefully });

Upvotes: 1

Related Questions