Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!