In the following search method, I just set the BehaviourSubject
value in the service. So, I am not sure if I can perform this without using subscribe()
or toPromise()
after .pipe()
block in the following method. But if not, which one should I use? And, assuming that there is no validation for the search input, should I get the response or subscribe to the result of setting this value?
search() {
fromEvent(this.searchInput.nativeElement, 'keyup').pipe(
// get value
map((event: any) => {
return event.target.value;
}),
debounceTime(1000),
distinctUntilChanged()
)
// subscribe ?
.subscribe(x => {
this.searchService.setSubject(x);
});
// toPromise ?
.toPromise().then(x => {
this.searchService.setSubject(x);
});
}
That depends on you.
Subscribing (using Observables) are preferred if you want to do more than just 1 action and they are cancelable.
Promises handle only a single event.
See: Difference of Observables and Promises
Personally, I use promises if its something that is simple such as fetching data and catching any errors then displaying the error messages.