I'm currently putting together a chat application using socket.io in Angular. I've encountered an issue where I can't seem to execute a particular code or function right after an observable variable is initialized through subscription. The intention is for this code, once initialized, to automatically scroll down to show the most recent message, but it's firing too soon.
My suspicion is that the issue lies in the fact that the chatList
variable below hasn't had enough time to populate with the list of messages.
While using
setTimeout(() => {//scroll function here}, 1);
does work, it isn't the most optimal solution.
this.getMessages()
.subscribe((chat: string[]) => {
this.chatList$ = chat;
//I want the following code to be executed only once the above code is fully initialized.
this.conversationList.nativeElement.scrollTop = this.conversationList.nativeElement.scrollHeight;
});
Is there a correct method to ensure a specific code or function runs only when the observable has completed?