user2024080
user2024080

Reputation: 5101

`this.appInits[i] is not a function` error accures on `APP_INITIALIZER`

I am trying a simple APP_INITIALIZER with my angular app, but getting error as

this.appInits[i] is not a function - not able to figure out the issue.

here is my app module:

import { NgModule, APP_INITIALIZER  } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';


export function onAppInit1(){
  return new Promise((resolve,reject) => {
    return setTimeout(() => resolve(true), 5000);
  })
}

@NgModule({
  declarations: [
          AppComponent,
          SignInComponent
    ],
  imports: [

  ],
  providers: [
    {
      provide:APP_INITIALIZER,
      useFactory:onAppInit1,//getting error
      multi: true
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: InsertAuthTokenInterceptor,
      multi: true
    }
  ],

  bootstrap: [AppComponent]
})
export class AppModule { }

Upvotes: 5

Views: 5542

Answers (1)

enno.void
enno.void

Reputation: 6579

onAppInit1 should return a function that returns a promise

export function onAppInit1(){
  return ()  => { 
    return new Promise((resolve,reject) => {
      return setTimeout(() => resolve(true), 5000);
    })
  }
}

Upvotes: 19

Related Questions