Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda