Skip to content

Instantly share code, notes, and snippets.

@banujan6
Last active September 4, 2021 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banujan6/12f1a09a80cf168cde963246f36641ad to your computer and use it in GitHub Desktop.
Save banujan6/12f1a09a80cf168cde963246f36641ad to your computer and use it in GitHub Desktop.
Rxjs Medium
/**
First Example
**/
import { Observable } from 'rxjs';
// Observable Object
const observable = new Observable(subscriber => {
subscriber.next("First Message");
subscriber.next("Second Message");
});
// Observer Object
const observer = {
next: (data) => console.log('We Received new data ' + data),
error: (err) => console.error('Oh, No. We got an error : ' + err),
complete: () => console.log('Done. Nothing more.')
};
// Subscribing
const subscription = observable.subscribe(observer);
// Unsubscribing
subscription.unsubscribe();
/**
Second Example
**/
import { Observable } from 'rxjs';
// First Observable Object
const observable = new Observable(subscriber => {
subscriber.next("First Message");
});
// Second Observable Object
const observable2 = new Observable(subscriber => {
subscriber.next("Second Message");
});
// first subscription
const subscription = observable.subscribe(data => console.log("We got a data " + data));
// second subscription
const subscriptionChild = observable2.subscribe(data => console.log("We got a child data " + data));
// adding 2nd subscription as child of 1st
subscription.add(subscriptionChild);
// unsubscribing parent BUT child also will be unsubscribed
subscription.unsubscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment