Observables are a proposed feature for ES7 and are used for creating a continuous communication channel for various kinds of events. They add that communication channel by passing messages between publishers and subscribers. The unique thing about observables is that they are declarative—that is, once they are defined, they do not emit values unless they are subscribed to. One way of creating observables is by using subject. Let's see how it works by creating a new Subject:
const subject = new Subject();
// emitting two values before subscribing
subject.next(1);
// subscribing
subject.subscribe(value => console.log(value));
// emitting value after subscribing
subject.next(2)
In the preceding example, we have created a new instance of subject and then emitted a value using the next method. Then, we subscribed subject to listen for new values...