Subjects could be both an Observable and an Observer. They can be seen as a pushable Observable; they let you add more data to be propagated through them.
Subjects expose three important methods: onNext()
, onError()
, and onCompleted()
. These methods can be used to send events through the observable sequence. You can see their usage in the following example:
var subject = new Rx.Subject(); subject.subscribe( (message)=> console.log(message), (err)=>console.log('An error happened: '+err.message), ()=>console.log('END') ); subject.onNext('Hello World!!!'); subject.onCompleted();
In this example, we created a new subject
and subscribed to it, using the subscribe()
method. Then, we pushed data on this subject
using the onNext()
method, and we finally finished it calling the onCompleted()
method.
If you run this code, you will see the following output:
Hello World!!! END
We can also propagate an error through a subject using its onError()
method:
var...