facebook

use async service method as factory provider in angular 10

  1. CodeMix & Angular IDE
  2.  > 
  3. Getting Help
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #662947 Reply

    knowledgeplaces
    Participant

    I am developing an app with Angular 10 and I have to perform various initializations before the app is displayed to the user.

    I am new to Angular and Typescript and I am trying to do this based on samples found by googling the web.

    So far I have done this, which is supposed to make the app wait 10 seconds before launching, but it does not work yet.

    I have created an AdminutilsService with the following code:

    
    import { Injectable } from '@angular/core';
    
    @Injectable({
      providedIn: 'root'
    })
    
    export class AdminutilsService {
    
      constructor() { }
    
      loadCustomerConfig(): (() => Promise<boolean>) {
        return (): Promise<boolean> => {
          return new Promise<boolean>((resolve: (a: boolean) => void): void => {
            setTimeout(() => resolve(true), 10000);
          });
        };
      }
    
    }
    

    In app.module.ts, I have added:

    
    import { AdminutilsService } from './adminutils.service';
    export function loadCustomerConfigFactory(myAdminutilsService: AdminutilsService) {
         return () => myAdminutilsService.loadCustomerConfig();
    }
    

    and also in @NgModule:

    
      providers: [{
        provide: APP_INITIALIZER,
        useFactory: loadCustomerConfigFactory,
        multi: true
      }],
    

    Compile is done although I have a warning on line

    
    export function loadCustomerConfigFactory(AdminutilsService: AdminutilsService) {
    

    expected call-signature: ‘loadCustomerConfigFactory’ to have a typedef (typedef)

    But when I launch the app, there is not a 10 seconds delay as expected.

    Please advise.

Viewing 1 post (of 1 total)
Reply To: use async service method as factory provider in angular 10

You must be logged in to post in the forum log in