1 00:00:00,090 --> 00:00:05,250 In this lecture, we are going to explore the other functions we can add to an observer. 2 00:00:05,610 --> 00:00:11,330 The job of the observer is to read values as they are pushed in our script file. 3 00:00:11,370 --> 00:00:15,720 We're calling these subscribe function for registering a new observer. 4 00:00:16,140 --> 00:00:23,070 The object we're passing in can have up to three functions one function for handling emitted values, 5 00:00:23,280 --> 00:00:30,360 another function for handling errors, and lastly a function for handling the completion of an observable. 6 00:00:31,050 --> 00:00:36,360 The next function we're passing in will handle values pushed from the observable. 7 00:00:36,780 --> 00:00:40,650 We can push multiple values over time from an observable. 8 00:00:41,190 --> 00:00:45,240 Hence, why it's sometimes referred to as a stream of data. 9 00:00:45,630 --> 00:00:52,230 For example, inside the observable, we will call the next function with a different message. 10 00:00:54,820 --> 00:01:01,450 Inside the console, two separate logs are made, the function we passed into the subscribe function 11 00:01:01,450 --> 00:01:02,950 is being called twice. 12 00:01:03,400 --> 00:01:05,260 That's important to understand. 13 00:01:05,530 --> 00:01:08,530 Observables do not push data all at once. 14 00:01:08,830 --> 00:01:12,220 This behavior allows us to push data over time. 15 00:01:12,850 --> 00:01:15,520 At the moment, we have a memory leak issue. 16 00:01:15,790 --> 00:01:18,130 Observables are constantly running. 17 00:01:18,400 --> 00:01:20,590 They are not automatically terminated. 18 00:01:20,950 --> 00:01:26,560 Our SJS has a couple of functions for performing this task, which we will explore later. 19 00:01:26,890 --> 00:01:33,070 For now, let's learn how to manually terminate an observable inside the observable. 20 00:01:33,160 --> 00:01:37,600 We are going to call the complete function on the subscriber object. 21 00:01:40,110 --> 00:01:46,650 The complete function will prevent the observable from pushing new data, for example, nothing would 22 00:01:46,650 --> 00:01:51,870 happen if we attempted to call the next function after calling the complete function. 23 00:01:54,460 --> 00:02:01,270 As we can see in the console, the message in the third next function does not get logged inside the 24 00:02:01,270 --> 00:02:01,990 observer. 25 00:02:02,230 --> 00:02:04,720 We can run a function on completion. 26 00:02:05,110 --> 00:02:07,480 Let's define a method called complete. 27 00:02:10,150 --> 00:02:16,000 By calling the complete function, observers will be notified of the observables completion. 28 00:02:16,420 --> 00:02:18,820 This function isn't passed a value. 29 00:02:19,060 --> 00:02:23,530 Let's log a message to help us verify if the observable is complete. 30 00:02:26,250 --> 00:02:30,390 Inside the console, the message has been logged from our observer. 31 00:02:30,780 --> 00:02:34,470 There's one less function we are going to add to our observer. 32 00:02:34,800 --> 00:02:37,710 We can handle errors by adding the error function. 33 00:02:38,070 --> 00:02:41,670 This function will have one argument, which is called error. 34 00:02:44,240 --> 00:02:50,510 The error argument will store the error produced by the observable, we will log the error with the 35 00:02:50,510 --> 00:02:52,640 console port error function. 36 00:02:55,190 --> 00:03:01,970 Unlike the log function, the error function will produce a flaw with a red background after making 37 00:03:01,970 --> 00:03:02,900 those changes. 38 00:03:03,080 --> 00:03:06,230 Let's update our observable to throw an error. 39 00:03:06,590 --> 00:03:13,880 We will call the subscriber error function after the second next function with a basic error message. 40 00:03:16,420 --> 00:03:20,380 Inside the console, the air gets logged with our message. 41 00:03:20,620 --> 00:03:24,880 However, the message from our complete function never gets logged. 42 00:03:25,240 --> 00:03:30,730 If an error is thrown from an observable orange juice will terminate the observable. 43 00:03:31,090 --> 00:03:35,410 We won't be able to emit new values or complete the observable. 44 00:03:35,770 --> 00:03:41,380 Always keep this in mind when working observables other than calling the complete function. 45 00:03:41,530 --> 00:03:43,810 Errors can terminate an observable. 46 00:03:44,350 --> 00:03:46,600 There's one less thing I want to mention. 47 00:03:46,810 --> 00:03:48,580 Observers can be partial. 48 00:03:48,910 --> 00:03:52,660 We don't have to define all three functions in our observer. 49 00:03:52,930 --> 00:03:57,370 They are completely optional if we forget to define the other functions. 50 00:03:57,580 --> 00:03:59,650 Our SJS will not complain. 51 00:04:00,040 --> 00:04:06,760 It will run dummy functions for handling these events, which won't do anything before moving on to 52 00:04:06,760 --> 00:04:07,780 the next lecture. 53 00:04:07,930 --> 00:04:11,110 I'm going to completely empty out the observable. 54 00:04:13,640 --> 00:04:19,880 In the next lecture, we will look at how to handle asynchronous operations from within and observable.