In my current project I have an observable returning values and I have to update my local variable according to the value returned by the observable.
I see two ways of doing this, either by directly subscribing to my observable, or by using pipable operator.
According to your experience/knowledge, what is the best practice ?
1st approach (directly subscribing):
this.myObservable$.subscribe(value=> {
this.myValue = value;
});
2nd approach (using pipable operator):
this.myObservable$.pipe(
tap(value=> this.myValue = value)
).subscribe();
I think both are valid, but in this particular instance I would use the 1st approach and do the work in the subscribe block. The reason being that the intent is clearer.
"tap" is for doing side-effects, but you are not doing a side-effect here as there is no more logic going on after the tap. What you are doing is using the unwrapped final value to do something, and that is what the subscribe block is for.