Getting to started I've already included my RxJS script. I have a console clear to keep my console from being junked every time I run. I've switched JS Bin to 6 to 5 or Babel so I have access to arrow functions and other ES6 features.
I'm going to call then on it, and we'll just log out the value we get back. There's a slight delay, and then we see 42. To show the delay, find a log to show when the promise is starting. Again, promise started, 42.
To show this exact same thing in an Observable, I'm pretty much going to copy and paste the same code. Only this time I'm going to create a RxJS Observable. I'm not going to have a resolve function. Instead I'm going to have an observer that I call next on. Of course, my Observable is started.
I use for each instead of then. When I run this you see they both start, and they both emit their values. They're very similar at this point. One really important difference can be shown if I comment out my then and my for each.
This difference is that Observables are lazy and Promises are not. What I mean by lazy is when I run this you can see the promise started. It actually hit this block of code and ran the entire thing, which means my timeout actually did resolve. I'll run it again. It hit the success handler or the handler for my timeout where this block here wasn't executed at all.
Why is that interesting? That is interesting because if I have a promise it's always going to fire the block that's inside of here. If it's setting up some asynchronous call, the instant you have a promise it's going to fire that asynchronous call, be it Ajax, setting up some event handler, or what have you. That is already done by the time you have access to the promise.
If I have this Observable, this source, the Observable actually embodies everything that's necessary to set up its own data source. What kind of advantage do we have with Observables having this lazy feature? Another thing that Observables do is they actually embody not only the setup, this bit here, for their own data source, but the teardown.
If I was to add this exact some logging message to my timeout in my Observable -- we'll make it unique -- and I were to track the ida from my set timeout and return a function here that clears that time out, this actually will be called to teardown my Observable if I cancel it. Let's make these delays just a little bit longer, one second.
Now when I subscribe to these things I'm going to get my disposable that is returned from my for each subscription. I'll set a slightly quicker timeout and call dispose on it. What this is going to do is about a half second as opposed to a second here. I'm going to call dispose, and that's going to call what's inside of this function.
We know it's been called. I can run this, and you'll see it's started my Observable but then it called dispose. My promise timeout was hit, which is this guy up here, but not my Observable. To better illustrate this, let me comment out this promise and run this again.
You'll see it started my Observable, which means it hit this, called all this here. But then dispose was called here first. It called "My disposal method" which cleared my timeout, which means that this timeout never got called and never emitted a value.
Observables can actually embody the setup and teardown for their data sources. What does that mean? That means, unlike Promises, Observables can be canceled because they embody everything that's necessary to tear themselves down.
Likewise -- because Observables embody everything that's necessary to set themselves up -- Observables can be called again as long as you have access to them. If I were just to have this variable here, this Promise, and I called then again, it's not going to run this code again. It's just going to return the same value it already had.
That means if I have an Observable that's errored, I can simply subscribe to it again to retry it or use one of the retry operators such as "Retry or retryWhen." Whereas with a Promise if the Promise has errored, I better have access to whatever function returned the Promise because the Promise has already executed its own logic.
More importantly, the logic to create the Promise is already executed. It is a read only view to a future and cannot be repeated or retried.