I need to wait for my first function results before execute second one.
code
First function
getUser() {
this.authService.user().subscribe(
user => {
this.user = user;
console.log('current user 1: ', user);
console.log('current user 2: ', this.user);
}
);
}
async ngOnInit() {
this.socket.fromEvent('message').subscribe(async (message: any) => {
if(this.user === undefined) {
// first function called
this.getUser();
// before it finish this console log will fire, therefore it returns `undefined`
console.log('this user: ', this.user);
} else {
//....
}
}
}
I need to hold my console call till this.getUser(); is done. How to do that?