Questions tagged [rxjs-observables]
759 questions
1010
votes
14 answers
What is the difference between BehaviorSubject and Observable?
I'm looking into the design patterns of RxJS, and I do not understand the difference between BehaviorSubject and Observable.
From my understanding, BehaviorSubject can contain a value that may change. It can be subscribed to and subscribers can…

Kevin Mark
- 10,173
- 3
- 10
- 10
37
votes
8 answers
How to pass results between chained observables
Abstract problem: Every time a source Observable emits and event, a sequence of API calls and Angular services need to be triggered. Some of those invocations are depending on previous results.
In my example, the source Observable startUpload$…

d0x
- 11,040
- 17
- 69
- 104
16
votes
3 answers
how to unsubscribe a RXJS subscription inside the subscribe method?
I have some javascript:
this.mySubscription = someObservable.subscribe((obs: any) => {
this.mySubscription.unsubscribe();
this.mySubscription = undefined;
}
on execution, the console logs the error ERROR TypeError: Cannot read property…

MojioMS
- 1,583
- 4
- 17
- 42
14
votes
2 answers
In Jest, how can I unit test a method that subscribes to an observable
I have various methods in my components that subscribe to methods in injected dependencies, that return observables.
I want to write Jest unit tests to ensure that when these observables return / error, my methods do the correct thing.
In the below…

TheRyanSmee
- 231
- 1
- 2
- 9
8
votes
6 answers
ObjectUnsubscribedErrorImpl on an observable in a template in Angular
I am using Angular 11 and I am accessing an observable in the template of my component with async pipe.
First load of the route, everything works perfectly fine. No error. When I navigate away from the page and come back, I get following…

Ankit Tanna
- 1,779
- 8
- 32
- 59
7
votes
6 answers
Angular better way to clear subscriptions
There are many ways to handle multiple subscriptions efficiently in a component, I have 2 ways here and wanted to know which is more efficient and why??
Method 1: Using Array
Step 1: creating Array
private subscriptionArray: Subscription[];
Step 2:…

Varun Sukheja
- 6,170
- 5
- 51
- 93
5
votes
6 answers
How to wait for a consistent state in RxJS?
I have a code to fetch book and library card associated with it:
// mimic http requests
const fetchBook = (bookId: number) => {
const title = 'Book' + bookId;
return timer(200).pipe(mapTo({ bookId, title }));
}
const fetchLibraryCard =…

Artem
- 1,773
- 12
- 30
5
votes
1 answer
Mapping through an observable and return values of nested observable
hope you can help me out.
I have a function called getEnergyFromPower which returns a string of values:
getEnergyFromPower(systemId: number, objectId?: string) {
...
return this.httpClient.get(this.BASE_URL + this.AGGREGATE_URL, {
…

Yuri Rech
- 55
- 4
5
votes
2 answers
ngIf Async Pipe as value with only null check
I have an observable that I would like to create a variable with in the ngIf as well as only return false if the value is null (the observable returns a number)
I need to explicitly check for null as my observable can return 0 as a value which…

iamsimonsmale
- 366
- 3
- 14
5
votes
1 answer
Synchronous blocking subscribe call to a custom ReplaySubject based observable
The view contains the element:
Hello
When calling the component method:
downloadDemo(): void {
this.download$ = this.downloadService.downloadUrlAsBlobWithProgressAndSaveInFile('assets/skypeforlinux-64.deb', 'demo')
…
Stephane
- 11,836
- 25
- 112
- 175
5
votes
1 answer
Observer pattern design and RXJS library
I am a novice user in the library RXJS and trying to figure out how to use properly Observable and Subjects.
I am trying to draw parallels with pattern design Observer.
At some point, I have a question if the instance of the Observable from the…

Michael
- 13,950
- 57
- 145
- 288
5
votes
6 answers
Angular async lazy rendering with RxJS
I am looking for away to do "lazy rendering" with RxJS in Angular, what I want to achieve is the following:
{{item.text}}
and in the component I have:
export class ItemsComponent implements OnInit…

Sabri Aziri
- 4,084
- 5
- 30
- 45
5
votes
1 answer
Detect when a Subject has no more subscriptions
I'm implementing an angular service that lets consumers observe various values based on their id:
The essence of it looks like this:
private subjects = new Map>();
public subscribe(id: number, observer: any): Subscription {
…

Robert Hegner
- 9,014
- 7
- 62
- 98
5
votes
2 answers
What is the difference between merge and mergeAll?
What is the difference between merge and mergeAll? They both seem identical to…

Rayhan Muktader
- 2,038
- 2
- 15
- 32
5
votes
4 answers
Using Angular Observable to subscribe to Session Storage key
I'm trying to accomplish what I had hoped would be a simple "look at the key in Session Storage and update it when it changes" scenario, but Observables really trip me up.
message$ = new Observable(observer => {
…

Neurothustra
- 300
- 1
- 5
- 14