Creating Raw Observables
In this lesson, we'll look at how we can create an observable from scratch.
We'll cover the following...
In the last example, we used the fromEvent()
function to assist us in creating an observable. It makes an observable out of an element where it emits the event if the input
event is triggered. We’re able to create an observable ourselves if we need more control over when an event is emitted.
The Observable
object
We’ll be starting from scratch. A low-level observable can be created by creating a new instance of the Observable
object. Let’s see what that looks like.
Press + to interact
const { Observable } = rxjs;const observable = new Observable(observer => {observer.next('Hello!');observer.complete();observer.error(new Error('An error has occurred.'));});
In the example above, we’re grabbing the Observable
object from rxjs
. We’re creating a new instance out of it. We can ...