Check out a free preview of the full Advanced Asynchronous JavaScript course

The "Q&A: Hot Observables" Lesson is part of the full, Advanced Asynchronous JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Jafar codes a demonstration to answer students question about hot observables and timed events.


Transcript from the "Q&A: Hot Observables" Lesson

>> Jafar Husain: Yeah.
>> Speaker 2: I just have a quick question. Would it even make sense to have a hot observable or have a set timeout as a hot observable? Is that even possible?
>> Jafar Husain: Yeah, we could. Anybody have an idea how we could modify this to make it a hot observable?

So, well, you're asking us two questions, is it possible? And does it make sense? Is it possible? Absolutely. Does it make sense? Probably not.
>> Speaker 2: [LAUGH]
>> Jafar Husain: But you know what? Let's try it, cuz that's what learning is, right? And what I'm gonna do is I'm gonna pull this out, right?

>> Jafar Husain: And this is a little harder without first talking to you guys about subjects. But we can think about how we might accomplish something like this. So if I pull this out and run it when we call time out, this isn't gonna work. Why is it not gonna work as it is right now?

>> Speaker 2: You don't have access to observer within the, [INAUDIBLE] Function.
>> Jafar Husain: Why? The observer is defined here, right? Not up here, right? But what I could do is I could create,
>> Jafar Husain: A function. Let's create a Boolean variable.
>> Jafar Husain: Right? And when you subscribe,
>> Jafar Husain: I can say if (fired === true), then; right?

And observer.complete();
>> Jafar Husain: And I'm gonna create a handle for our observer, which we don't have yet.
>> Jafar Husain: And then once I get the real observer, I'm gonna assign it to this placeholder.
>> Jafar Husain: So we're gonna see why that's gonna work. So we create a little Boolean that tells us, hey, has this fired yet, right?

And then once this fires, we set it to true. If somebody decides to subscribe later, we just check if it's fired, and then we send it to them, right? But if they come in before it's fired, they just set this placeholder to their real observer, and then when we come in, we fire at it, make sense?

So technically the interesting thing is this is still a cold observable. Why is it a cold observable?
>> Speaker 2: Cuz the values are the same on both next and complete.
>> Jafar Husain: Well the point is you're close, we're never gonna miss the notification, right? No matter what happens, whenever we subscribe, we're always gonna get the notification cuz we're remembering, restoring, that we've actually fired.

If I was to do something simple like this, though.
>> Jafar Husain: Now we're not a cold observable anymore, right? Now we've got erase, unless we subscribe before the timeout fires, we're gonna miss it, and now we're hot observable. Does everybody understand the difference between a cold and hot observable, right?

So the answer to, why would I wanna create erase though, right? That's the question, right? I mean, why create a erase if you don't have to create a erase? So, that's why I probably wouldn't view timeout as a hot observable. Make sense? Now there might be some cases where we do want a hot observable, and we'll get into those later I think.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now