1 00:00:00,120 --> 00:00:05,670 In this lecture, we are going to unsubscribe from an observable from the observer. 2 00:00:05,939 --> 00:00:12,630 Previously, we saw how to run a function when an observable completes, it's considered good practice 3 00:00:12,630 --> 00:00:15,960 to complete an observable whenever we don't need it anymore. 4 00:00:16,379 --> 00:00:20,250 On the other hand, I wonder if we want an observable to run forever. 5 00:00:21,000 --> 00:00:25,350 Different parts of our apps may need to subscribe to the same observable. 6 00:00:25,740 --> 00:00:31,620 If an observer doesn't need to listen to data from an observable anymore, we should unsubscribe from 7 00:00:31,620 --> 00:00:34,950 the observable without interrupting other observers. 8 00:00:35,280 --> 00:00:40,560 It's completely possible for a single observer to unsubscribe from an observable. 9 00:00:40,950 --> 00:00:48,130 This behavior allows for other observers to continue receiving data uninterrupted in our file. 10 00:00:48,150 --> 00:00:50,910 We are going to remove the complete function. 11 00:00:53,510 --> 00:00:55,730 The observable should run forever. 12 00:00:56,030 --> 00:01:02,060 Instead of completing the observable, we will unsubscribe from the observable with our observer. 13 00:01:02,390 --> 00:01:07,280 Let's hover our mouse over the subscribed function, according to the editor. 14 00:01:07,430 --> 00:01:11,420 These subscribed functional returns an object called subscription. 15 00:01:11,780 --> 00:01:18,020 In the resource section of this lecture, I provide a link to the subscription object documentation 16 00:01:18,020 --> 00:01:18,590 page. 17 00:01:19,940 --> 00:01:26,510 The description says the following a subscription is an object that represents a disposable resource. 18 00:01:26,720 --> 00:01:34,430 Usually, the execution of an observable a subscription has one important method unsubscribe that takes 19 00:01:34,430 --> 00:01:38,990 no argument and just disposes the resource held by the subscription. 20 00:01:39,650 --> 00:01:45,950 The subscription object doesn't serve much of a purpose besides providing a function for unsubscribing 21 00:01:45,950 --> 00:01:47,090 from an observable. 22 00:01:47,450 --> 00:01:49,940 It's precisely the function we're looking for. 23 00:01:50,240 --> 00:01:51,980 Let's go back to the editor. 24 00:01:52,980 --> 00:01:55,410 We should store the subscription object. 25 00:01:55,710 --> 00:01:59,370 We will store the object in a variable called subscription. 26 00:02:01,860 --> 00:02:09,240 Below the Subscribe function, we will call the set timeout function with a duration of 4000 milliseconds. 27 00:02:11,660 --> 00:02:17,900 Inside the callback function, we will call the unsubscribe function on the subscription object. 28 00:02:20,400 --> 00:02:26,790 In the console, the message is we'll continue to get launched after four seconds, they will cease 29 00:02:26,790 --> 00:02:28,140 appearing in the log. 30 00:02:28,470 --> 00:02:33,540 Not only has the subscription been cancelled, but the interval has been released. 31 00:02:33,870 --> 00:02:39,930 The function were returning from the observable will be executed when the observable completes or an 32 00:02:39,930 --> 00:02:41,400 observer unsubscribe. 33 00:02:41,940 --> 00:02:46,230 In either scenario, we are given the opportunity to release memory. 34 00:02:46,830 --> 00:02:52,620 You may have noticed it already, but the complete function from our observer never runs. 35 00:02:52,920 --> 00:02:56,760 Hence, we can assume the observable has not completed. 36 00:02:57,120 --> 00:03:00,250 It's exactly what we wanted in the console. 37 00:03:00,270 --> 00:03:03,480 The message from the complete function never appears. 38 00:03:04,050 --> 00:03:08,730 Other observers can continue to listen for new data from the observable. 39 00:03:09,090 --> 00:03:16,230 Our SJS gives us flexibility with managing observables and observers before moving on. 40 00:03:16,410 --> 00:03:18,780 I want to clean up the code in our file. 41 00:03:19,020 --> 00:03:23,370 Let's remove the console statements before and after the subscription. 42 00:03:25,970 --> 00:03:32,330 In the following lecture, we're going to start discussing operators for helping us write observables.