In this lecture, we are going to start using operators to help us write observables.
We're going to focus on creation operators for the next couple of lectures.
As a refresher, a creation operator is a function for creating an observable.
It will act as a wrapper around a producer of data such as events, timers or a combination of other
observables creation.
Operators make it possible to create observables from any source of data.
We will be exploring the most commonly used operators in our jobs.
The first operator we're going to explore is called the interval operator inside our code.
We're manually creating an interval with the set interval function.
Writing an observable from scratch is tedious.
We can simplify it with the interval operator at the top of the file.
We're going to update the important statement for the SJS package instead of importing the observable
class.
We will import an operator called Interval.
Next, we will completely replace the instance of the observable class with the interval operator.
The interval operator will perform a couple of tasks.
Firstly, it'll create an interval similar to these set interval function.
This interval will be wrapped with an observable.
The observable is returned as a value.
We can verify the return value by hovering our mouse over the interval function.
The return value of this function is annotated as an observable with a single function.
We will accomplish the same solution we had previously.
We've gone from an imperative solution to a declarative solution.
The interval operator will emit a stream of values based on the duration passed into the function.
That aeration must be in milliseconds.
Let's pass in one thousand.
By default, intervals will limit numbers in ascending order.
If we look inside the console, the numbers start at zero.
Going up to four, the interval stops because we're unsubscribing from the observable after four seconds.
It's worth mentioning the interval will go on forever.
It never completes.
Let's remove the set timeout function altogether.
As you can see, the observable will continue to emit values.
We can simplify our code even further.
At the moment, we're passing in an object.
Alternatively, we can pass in a single function, passing at a single function to the subscribe function.
It will be registered as the next function.
This shorthand syntax for creating an observer it can be useful if you're not concerned with completing
an observable or its errors.
Let's replace the object with the console log function.
Everything has become much more readable.
Back to the task at hand.
The interval operator can be helpful if you need to create a countdown or delay an event.
There are some limitations with the interval operator.
It'll continuously emit values.
However, we may want to emit one value.
Secondly, we may want to emit a value immediately without delay for subsequent values.
There's another operator with more options for customizing the interval in our file.
Let's swap the interval operator with the timer operator.
The timer operator offers more flexibility than the interval operator.
They're very similar.
Just like the interval operator, the timer operator will emit a stream of numbers to the observer inside
the console.
A single number has been emitted.
The first argument of the timer operator is a timer for the first value it emits.
We can configure when the interval should begin.
In this example, we are starting the interval after one seconds if we want to begin emitting values
immediately.
We can pass in zero.
The timer operator will emit one value we can emit values in intervals by passing in a duration as the
second argument to the function by omitting the second argument.
The timer operator will assume we want to emit a single value.
Let's pass in one thousand.
This time, the operator is emitting values every second, the duration in the second argument applies
to subsequent values.
It does not apply to the first value emitted by the observable with the timer operator.
We can fine tune the delay of each value.
In general, it's not necessary to manually create an observable.
Our SJS is a powerful library packed with numerous operators to help us create observables we can jumpstart
in observable with a single operator in the next lecture.
We will continue learning about creation operators.