Skip to content

APP_INITIALIZER won't work with delayed promises #1487

@Peluko

Description

@Peluko

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:

let bootstrapPromiseCompleted = false;
this._bootstrapper().then(
moduleRef => {
bootstrapPromiseCompleted = true;
bootstrapLog(`Angular bootstrap bootstrap done. uptime: ${uptime()}`);
if (!autoCreateFrame) {
rootContent = tempAppHostView.content;
}
lastBootstrappedModule = new WeakRef(moduleRef);
},
err => {
bootstrapPromiseCompleted = true;
const errorMessage = err.message + "\n\n" + err.stack;
bootstrapLogError("ERROR BOOTSTRAPPING ANGULAR");
bootstrapLogError(errorMessage);
rootContent = this.createErrorUI(errorMessage);
}
);
bootstrapLog("bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
(<any>global).Zone.drainMicroTaskQueue();
bootstrapLog("bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
if (!bootstrapPromiseCompleted) {
const errorMessage = "Bootstrap promise didn't resolve";
bootstrapLogError(errorMessage);
rootContent = this.createErrorUI(errorMessage);
}

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions