One of the best-practice principles of Angular is to always use AsyncPipe when possible and only use .subscribe() when the side effect is an absolute necessity and cannot be avoided.

What Is an Observable?

An Observable is an abstraction of an asynchronous stream of data. For example, when we look at anObservable, it represents a stream of strings that will be delivered one by one over time. TK (https://medium.com/isop-nepal/subscribe-vs-async-pipe-in-angular-21bb38f3ee49)

But when the Observable gets modified, how do we display data? This is where we use .subscribe().

Subscribe Function

We pass the Observable around, combining it and saving it to different variables with different combinations of operators, but at the end, an Observable<T> is useless on its own. We need a way to “terminate” the Observable and extract the type T out of it. That is what .subscribe is used for: to subscribe to the resulting stream and terminate the observable. TK (https://kimsereyblog.blogspot.com/2018/05/async-pipe-versus-subscribe-in-angular.html)

#angular #javascript #observables #programming #rxjs

Angular: Avoid the Subscribe() Method for Observables
4.65 GEEK