▲ 3 r/angular
RxJS: are you supposed to use tap to set state in the service, when data is retrieved from an API?
What is the best practice when loading data from an API that you want to set in a service? I see two approaches:
public load()
{
return this.api.listItems().pipe(
tap((items) => {
this.items.set(items);
}),
switchMap(() => of(void 0)),
);
}
or
public load()
{
return this.api.listItems();
}
public set(items: Item[])
{
this.items.set(items);
}
The first feels easier to read but feels like I am hacking tap in a way it shouldn't be used?
The second seems more correct but feels like I have extra code?
u/AFulhamImmigrant — 1 day ago