1 00:00:00,270 --> 00:00:06,540 In this lecture, we are going to learn how to push asynchronous values from our observables. 2 00:00:06,870 --> 00:00:11,250 Up until now, our observable has been entirely synchronous. 3 00:00:11,820 --> 00:00:15,920 We can improve this by logging some values in the observable. 4 00:00:15,990 --> 00:00:19,560 We are going to push a new value with the next function. 5 00:00:22,070 --> 00:00:27,170 Afterward, we are going to log a message before subscribing to the observer of all. 6 00:00:31,030 --> 00:00:34,300 We will launch another message after the subscription. 7 00:00:36,920 --> 00:00:42,530 Looking inside the console, we find our message is logged in the order they are written. 8 00:00:42,890 --> 00:00:48,830 Typically, most observables you'll be working with are asynchronous at this point. 9 00:00:48,980 --> 00:00:52,100 We've got a good grasp of how observables work. 10 00:00:52,460 --> 00:00:57,680 We're going to start taking things a step further by working with asynchronous values. 11 00:00:58,160 --> 00:01:02,330 It's much more practical to use our SJS in this manner. 12 00:01:03,050 --> 00:01:05,209 Let's modify our observable. 13 00:01:05,510 --> 00:01:12,050 We will start with a simple example inside the function we will call the set interval function with 14 00:01:12,050 --> 00:01:14,630 a duration of 1000 milliseconds. 15 00:01:17,120 --> 00:01:21,740 Inside the interval, we will call the next function with a message. 16 00:01:24,360 --> 00:01:31,260 We're logging a message every seconds, the observer is constantly watching the observable for new values. 17 00:01:31,860 --> 00:01:36,120 As you can see, the council is continuously logging values. 18 00:01:36,420 --> 00:01:40,080 There are no limits as to how many values we can push. 19 00:01:40,410 --> 00:01:45,480 Our interval or run infinitely that much is easy to understand. 20 00:01:45,690 --> 00:01:47,850 However, we have a bigger problem. 21 00:01:48,180 --> 00:01:50,790 What if we want to complete the observable? 22 00:01:51,060 --> 00:01:53,460 Does that mean the interval will stop running? 23 00:01:53,760 --> 00:01:58,140 Not necessarily after calling the next function in the interval. 24 00:01:58,650 --> 00:02:02,550 Let's call the complete function on the subscriber object. 25 00:02:05,130 --> 00:02:12,000 In the log, the observer will stop logging values despite the observer completing we have a memory 26 00:02:12,000 --> 00:02:12,360 leak. 27 00:02:12,690 --> 00:02:15,970 The interval is still running inside the interval. 28 00:02:15,990 --> 00:02:17,520 We will log a message. 29 00:02:20,150 --> 00:02:27,230 After a few seconds, the council will continue to log messages completing an observable does not mean 30 00:02:27,230 --> 00:02:30,200 the contents of an observable have been cleared. 31 00:02:30,590 --> 00:02:33,740 Memory is still being allocated for the interval. 32 00:02:33,980 --> 00:02:38,090 We should free this memory if the observable is no longer running. 33 00:02:38,480 --> 00:02:40,500 This is important to understand. 34 00:02:40,790 --> 00:02:44,450 We are responsible for cleaning the memory in an observable. 35 00:02:44,810 --> 00:02:49,400 Otherwise, we may experience sluggish performance from memory leaks. 36 00:02:49,880 --> 00:02:53,480 We should clear the interval after completing the observable. 37 00:02:53,780 --> 00:02:59,750 We can return a function from an observable to handle cleaning up asynchronous operations. 38 00:03:02,300 --> 00:03:07,160 First, let's store the I.D. returned from the set interval function. 39 00:03:09,710 --> 00:03:13,730 Next, we can clear the interval with the clear interval function. 40 00:03:16,300 --> 00:03:23,500 After updating our file, the logs have stopped once the observable completes, the returned function 41 00:03:23,500 --> 00:03:26,170 will be invoked, which will clear the interval. 42 00:03:26,500 --> 00:03:28,870 This behavior can be tricky to deal with. 43 00:03:29,170 --> 00:03:31,900 Observables offer a lot of benefits. 44 00:03:32,080 --> 00:03:34,750 But we should be aware of situations like this. 45 00:03:35,020 --> 00:03:38,770 We will look at an alternative situation in the next lecture.