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();
}
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)
);
}