1 00:00:00,120 --> 00:00:05,130 In this lecture, we are going to start using operators to help us write observables. 2 00:00:05,400 --> 00:00:09,810 We're going to focus on creation operators for the next couple of lectures. 3 00:00:10,200 --> 00:00:15,510 As a refresher, a creation operator is a function for creating an observable. 4 00:00:15,750 --> 00:00:22,470 It will act as a wrapper around a producer of data such as events, timers or a combination of other 5 00:00:22,470 --> 00:00:24,300 observables creation. 6 00:00:24,300 --> 00:00:28,890 Operators make it possible to create observables from any source of data. 7 00:00:29,520 --> 00:00:34,280 We will be exploring the most commonly used operators in our jobs. 8 00:00:34,680 --> 00:00:40,660 The first operator we're going to explore is called the interval operator inside our code. 9 00:00:40,830 --> 00:00:45,090 We're manually creating an interval with the set interval function. 10 00:00:45,540 --> 00:00:48,390 Writing an observable from scratch is tedious. 11 00:00:48,570 --> 00:00:53,520 We can simplify it with the interval operator at the top of the file. 12 00:00:53,550 --> 00:01:00,390 We're going to update the important statement for the SJS package instead of importing the observable 13 00:01:00,390 --> 00:01:00,930 class. 14 00:01:01,140 --> 00:01:04,170 We will import an operator called Interval. 15 00:01:06,720 --> 00:01:13,230 Next, we will completely replace the instance of the observable class with the interval operator. 16 00:01:15,790 --> 00:01:19,120 The interval operator will perform a couple of tasks. 17 00:01:19,360 --> 00:01:24,070 Firstly, it'll create an interval similar to these set interval function. 18 00:01:24,460 --> 00:01:27,160 This interval will be wrapped with an observable. 19 00:01:27,490 --> 00:01:30,250 The observable is returned as a value. 20 00:01:30,400 --> 00:01:35,800 We can verify the return value by hovering our mouse over the interval function. 21 00:01:36,340 --> 00:01:42,310 The return value of this function is annotated as an observable with a single function. 22 00:01:42,460 --> 00:01:46,120 We will accomplish the same solution we had previously. 23 00:01:46,450 --> 00:01:50,290 We've gone from an imperative solution to a declarative solution. 24 00:01:50,830 --> 00:01:57,160 The interval operator will emit a stream of values based on the duration passed into the function. 25 00:01:57,580 --> 00:01:59,950 That aeration must be in milliseconds. 26 00:02:00,160 --> 00:02:01,960 Let's pass in one thousand. 27 00:02:04,600 --> 00:02:08,680 By default, intervals will limit numbers in ascending order. 28 00:02:09,220 --> 00:02:12,880 If we look inside the console, the numbers start at zero. 29 00:02:12,910 --> 00:02:19,690 Going up to four, the interval stops because we're unsubscribing from the observable after four seconds. 30 00:02:20,020 --> 00:02:23,050 It's worth mentioning the interval will go on forever. 31 00:02:23,290 --> 00:02:24,520 It never completes. 32 00:02:24,790 --> 00:02:28,060 Let's remove the set timeout function altogether. 33 00:02:30,540 --> 00:02:34,860 As you can see, the observable will continue to emit values. 34 00:02:35,070 --> 00:02:37,590 We can simplify our code even further. 35 00:02:37,860 --> 00:02:40,560 At the moment, we're passing in an object. 36 00:02:40,920 --> 00:02:47,520 Alternatively, we can pass in a single function, passing at a single function to the subscribe function. 37 00:02:47,520 --> 00:02:50,130 It will be registered as the next function. 38 00:02:50,550 --> 00:02:56,820 This shorthand syntax for creating an observer it can be useful if you're not concerned with completing 39 00:02:56,820 --> 00:02:58,950 an observable or its errors. 40 00:02:59,580 --> 00:03:03,420 Let's replace the object with the console log function. 41 00:03:05,990 --> 00:03:08,450 Everything has become much more readable. 42 00:03:08,630 --> 00:03:10,250 Back to the task at hand. 43 00:03:10,610 --> 00:03:16,430 The interval operator can be helpful if you need to create a countdown or delay an event. 44 00:03:16,880 --> 00:03:19,970 There are some limitations with the interval operator. 45 00:03:20,240 --> 00:03:22,650 It'll continuously emit values. 46 00:03:22,910 --> 00:03:25,880 However, we may want to emit one value. 47 00:03:26,180 --> 00:03:32,510 Secondly, we may want to emit a value immediately without delay for subsequent values. 48 00:03:33,050 --> 00:03:38,790 There's another operator with more options for customizing the interval in our file. 49 00:03:38,840 --> 00:03:42,830 Let's swap the interval operator with the timer operator. 50 00:03:45,400 --> 00:03:50,290 The timer operator offers more flexibility than the interval operator. 51 00:03:50,680 --> 00:03:51,850 They're very similar. 52 00:03:52,060 --> 00:03:59,140 Just like the interval operator, the timer operator will emit a stream of numbers to the observer inside 53 00:03:59,140 --> 00:03:59,980 the console. 54 00:04:00,250 --> 00:04:02,320 A single number has been emitted. 55 00:04:02,890 --> 00:04:08,620 The first argument of the timer operator is a timer for the first value it emits. 56 00:04:08,950 --> 00:04:12,010 We can configure when the interval should begin. 57 00:04:12,370 --> 00:04:18,910 In this example, we are starting the interval after one seconds if we want to begin emitting values 58 00:04:18,910 --> 00:04:19,779 immediately. 59 00:04:19,810 --> 00:04:21,430 We can pass in zero. 60 00:04:24,000 --> 00:04:31,320 The timer operator will emit one value we can emit values in intervals by passing in a duration as the 61 00:04:31,320 --> 00:04:35,490 second argument to the function by omitting the second argument. 62 00:04:35,730 --> 00:04:39,750 The timer operator will assume we want to emit a single value. 63 00:04:40,020 --> 00:04:41,820 Let's pass in one thousand. 64 00:04:44,460 --> 00:04:51,540 This time, the operator is emitting values every second, the duration in the second argument applies 65 00:04:51,540 --> 00:04:53,130 to subsequent values. 66 00:04:53,430 --> 00:04:59,550 It does not apply to the first value emitted by the observable with the timer operator. 67 00:04:59,670 --> 00:05:02,730 We can fine tune the delay of each value. 68 00:05:03,300 --> 00:05:07,920 In general, it's not necessary to manually create an observable. 69 00:05:08,250 --> 00:05:16,440 Our SJS is a powerful library packed with numerous operators to help us create observables we can jumpstart 70 00:05:16,440 --> 00:05:20,550 in observable with a single operator in the next lecture. 71 00:05:20,670 --> 00:05:24,180 We will continue learning about creation operators.