-
-
Notifications
You must be signed in to change notification settings - Fork 241
Description
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
:
nativescript-angular/nativescript-angular/platform-common.ts
Lines 165 to 197 in 5e8c092
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.