I have a situation where I need to wait for the results of my first function before executing the 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) {
// call the first function
this.getUser();
// this console log will fire before the function finishes, resulting in 'undefined'
console.log('this user: ', this.user);
} else {
//....
}
}
}
I need to delay my console call until this.getUser();
is completed. What is the best way to achieve this?