Using Observables
First we import
Observable
into our component from rxjs/Observable
. Next, in our constructor we create a new Observable
. Note that this creates an Observable
data type that contains data of type. This illustrates the stream of data that Observables
offer as well as giving us the ability to maintain integrity of the type of data we are expecting to receive.Next we call
subscribe
on this Observable
which allows us to listen in on any data that is coming through. In subscribing we use three distinctive callbacks: the first one is invoked when receiving new values, the second for any errors that arise and the last represents the function to be invoked when the sequence of incoming data is complete and successful.We can also use
forEach
to listen for incoming data. The key difference between and subscribe
is in how the error and completion callbacks are handled. The forEach
call only accepts the 'next value' callback as an argument; it then returns a promise instead of a subscription.When the
Observable
completes, the promise resolves. When the Observable
encounters an error, the promise is rejected.You can think of as being semantically equivalent to:
The
forEach
pattern is useful for a sequence of events you only expect to happen once.View Example