2
votes

I do not understand why Angular Tutorial does not update the heroes list inside .subscribe().

Here is the quote from the tutorial:

Although the component delegates hero deletion to the HeroService, it remains responsible for updating its own list of heroes. The component's delete() method immediately removes the hero-to-delete from that list, anticipating that the HeroService will succeed on the server. There's really nothing for the component to do with the Observable returned by heroService.delete() but it must subscribe anyway.

Should it be better to do the heroes list update inside the .subscribe() because at that time we are sure that the server successfully delete the hero?

I think making assumption that "the HeroService will succeed on the server" is not good because it is possible that we update our local heroes list, and the server's deletion is not successful.

delete(hero: Hero): void {
  this.heroes = this.heroes.filter(h => h !== hero);
  this.heroService.deleteHero(hero).subscribe();
}
1

1 Answers

0
votes

It depends on how you want your application to be, a optimistic or pessimistic one. Thank you @R. Richards :)

Basically you've got two options,

  1. update immediately, and rollback on failure
  2. update after success.

With the first option you've got a much quicker user experience, the 2nd one is more 'true'