I have this code can you please explain what does TAP function doing here?
resend(id: number): void {
this.orderService.resend(id).pipe(tap(() => { // TAP ()
this.snackBarService.successMessage('table.resend_success_message');
},
error => this.snackBarService.errorMessage(),
)).subscribe(() => {
return this.getList(this.params);
});
}
CodePudding user response:
The comment of @Eliseo is correct but I will try to give you an example. In your case it's the same thing as doing this:
this.orderService.resend(id).subscribe(() => {
this.snackBarService.successMessage('table.resend_success_message');
return this.getList(this.params);
});
But this approach has some downside. If you want to subscribe to this.orderService.resend(id)
from many places, you alway need to call this.snackBarService.successMessage
. This is bad practice because we don't want to rewrite the same code multiple timse.
So a solution would be this:
resend$(id):Observable<any> { // this will return your list
return this.orderService.resend(id).pipe(
tap(() => this.snackBarService.successMessage('table.resend_success_message')),
map(() => this.getList(this.params))
);
});
test(){
this.resend$(1).subscribe(
myList => {
// here you will have the list, no need to successMessage()
}
);
}
as you can see we could call this.resend$(1).subscribe()
in multiple places and we don't need to call this.snackBarService.successMessage
anymore