nativescript-angular icon indicating copy to clipboard operation
nativescript-angular copied to clipboard

APP_INITIALIZER won't work with delayed promises

Open Peluko opened this issue 7 years ago • 13 comments

Make sure to check the existing issues in this repository

Checked.

If there is no issue for your problem, tell us about it

I'm trying Angular's APP_INITIALIZER to initialize app configuration before app launches (SQLite reading). Angular's documentation states that it will wait until the promises on APP_INITIALIZER are resolved to start the application. But on Nativescript you always obtain the error 'Bootstrap promise didn't resolve' whenever you use a delayed Promise on APP_INITIALIZER. This simple code produces the error:

function testInitializer(): Promise<void> {
    return new Promise((resolve: () => void) =>
        setTimeout(() => resolve(), 10)
    );
}

@NgModule({
    bootstrap: [
        AppComponent
    ],
    imports: [
        NativeScriptModule,
        AppRoutingModule
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        { provide: APP_INITIALIZER, useFactory: () => testInitializer, multi: true }
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class AppModule { }

However, if you return a resolved promise, it works. For example, if you change the initialization funtion to:

function testInitializer(): Promise<void> {
    return Promise.resolve();
}

it boots correctly.

I think that the problem is with the following code on platform-common.js, which doesn't wait for promises before checking the value of bootstrapPromiseCompleted:

https://github.com/NativeScript/nativescript-angular/blob/5e8c0921668a87f68a0f70b9622dc5a2c25a6224/nativescript-angular/platform-common.ts#L165-L197

Which platform(s) does your issue occur on?

  • iOS and Android, both emulator and device

Please, provide the following version numbers that your issue occurs with:

It can be easily reproduced on Playground: APP_INITIALIZER_PROMISES

Please, tell us how to recreate the issue in as much detail as possible.

The above mentioned Playground does it well.

Is there any code involved?

Peluko avatar Aug 15 '18 16:08 Peluko