Search⌘ K
AI Features

Creating Raw Observables

Explore how to create raw observables in Angular using the Observable object from RxJS. Learn to emit data with next(), complete observables, handle errors, and subscribe to observables for reactive programming control.

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.

Javascript (babel-node)
const { Observable } = require('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 ...