Skip to main content

Command Palette

Search for a command to run...

What is an observable

Published
1 min read
What is an observable
L

Passionate Software developer

An Observable is basically a function that can return a stream of values either synchronously or asynchronously to an observer over time. The consumer can get the value by calling subscribe() method. Let's look at a simple example of an Observable

import { Observable } from 'rxjs';

const observable = new Observable(observer => {
  setTimeout(() => {
    observer.next('Message from a Observable!');
  }, 3000);
});

observable.subscribe(value => console.log(value));

observables.png

P

Observables are also like callbacks and promises - that are responsible for handling async requests. Observables are a part of the RXJS library.

import { Observable } from 'rxjs';

const observable = new Observable(subscriber => {
  subscriber.next(1);
  subscriber.next(2);
  subscriber.next(3);
  setTimeout(() => {
    subscriber.next(4);
    subscriber.complete();
  }, 1000);
});

Observable Phases

  • Creation
  • Subscription.
  • Execution
  • Destruction.
1
L
Lymcode5y ago

Thank you. can we publish it here ?

More from this blog

L

Lymcode | A Step towards technology

8 posts

We are a Community of High-Class Developers specialised in building User Interfaces. we teach, build and share reusable Backend & frontend components, libraries, rules and style guides