Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
why APP_INITIALIZER of angular doesn't work

It's part of my project code. Before the app starts, the user service should verify the permissions through a function that checks the user's permissions. However, the APP_INITIALIZER function does not work properly. (The apiservice obtained the information with the get request and confirmed that there was no problem with the operation.) I'm wondering if there's a way to use Observable without using async/await.


// APP_INITIALIZER function 
function load(userService: UserService){
  () => {
    return userService.getUser();
  }
}

// userService - getUser()
this.user$ = new Behavior Subject(null);
getUser() {
  from(this.apiService.getUser()){
    user => this.user$.next(user)
  }
}


As I have tried it, it works well if you change the userService code as follows. However, I wonder if there is a way to do it without using toPromise, lastValueFrom.

// userService - getUser()
this.user$ = new Behavior Subject(null);
getUser() {
  const user = from(this.apiService.getUser());

  user.subscribe(user => this.user$.next(user));
  
  return user.toPromise();
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

why are you doing it so complicated? i don't think you need a behaviour subject. You can do it like this:

getUser():Observable<any> {
  return this.apiService.getUser();
}

and if you need to cache the user you can do it like this:

user:any = null;

getUser():Observable<any> {
  if(this.user){
    return of(this.user);
  }

  return this.apiService.getUser().pipe(
    tap(user => this.user = user)
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda