'Run tap() without subscribe

Is it possible to run the tap() pipe without subscribing?


observer$:BehaviorSubject<number[]> = new BehaviorSubject<number[]>([1])
getData(page:number):void{
    of(page).pipe(
        tap({
            next: (data) => this.observer$.next([...this.observer$.value, data])  
        })
    )
}

What I want is every time getData(val) is called, observer$'s value will be updated by [...observer$, val]



Solution 1:[1]

An observable will not emit values until you call subscribe. However, i don't see a reason why you need to create and then tap an observable to do what you're trying to do. I would just do this:

observer$:BehaviorSubject<number[]> = new BehaviorSubject<number[]>([1])
getData(page:number):void {
   this.observer$.next([...this.observer$.value, page])
}

EDIT: if you want to do an http request, you will need to subscribe to it to initiate the request:

getData(page: number): void {
  this.http.get('some url')
    .subscribe(data => {
      this.observer$.next([...this.observer$.value, data]);
    });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1