Skip to content

Instantly share code, notes, and snippets.

@vietduchelo
Created March 20, 2018 04:37
Show Gist options
  • Save vietduchelo/ffa380cb00a843b6ba22faae5ad6c7b2 to your computer and use it in GitHub Desktop.
Save vietduchelo/ffa380cb00a843b6ba22faae5ad6c7b2 to your computer and use it in GitHub Desktop.
// Behavior Subject
// a is an initial value. if there is a subscription
// after this, it would get "a" value immediately
let bSubject = new BehaviorSubject("a");
bSubject.next("b");
bSubject.subscribe((value) => {
console.log("Subscription got", value); // Subscription got b,
// ^ This would not happen
// for a generic observable
// or generic subject by default
});
bSubject.next("c"); // Subscription got c
bSubject.next("d"); // Subscription got d
Example 2 with regular subject:
// Regular Subject
let subject = new Subject();
subject.next("b");
subject.subscribe((value) => {
console.log("Subscription got", value); // Subscription wont get
// anything at this point
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment