Reputation: 1026
I would check that Observable has no element I do something like this
unusedElements$: Observable<Array<string>> = this.elements;
isEmpty(): boolean {
let empty = false;
this.unusedElements$.subscribe(res => res.length > 0 ? empty = !empty : empty).unsubscribe();
return empty;
}
I wonder this is the right way? Is maybe possible checking without subscribing if Observable has no element in Array?
Upvotes: 0
Views: 827
Reputation: 11345
if you program in an functional fashion, there are very few situations you will have to use something like isSomething() to return boolean value, unless it is useful for other stream to reuse.
Most of the time you can just use filter to silent the emission. In case you still want to subscribe to different case you can split stream like below and continue you work. There also a partition
operator you can use
const hasItem=unusedElements$.pipe(filter(res=>res.length>0))
hasItem.pipe(....do something)
const noItem=unusedElements$.pipe(filter(res=>res.length===0))
noItem.pipe(....do something)
functional programming is about break up your tasks and recompose them for business requirement
Upvotes: 2