BehaviorSubject of RxJS

Learn about BehaviorSubject and how it can be used to display placeholder text while waiting for an HTTP response.

When an Observer subscribes to a BehaviorSubject, it receives the last emitted value and then all the subsequent values. A BehaviorSubject requires us to provide a starting value so that all Observers always receive a value when they subscribe to a BehaviorSubject.

Suppose that we want to retrieve a remote file and print its contents on an HTML page, but we want to display some placeholder text while we wait for the contents. We can use a BehaviorSubject for this:

Get hands-on with 1400+ tech skills courses.