I've been facing a few alternatives in retrieving data from a simple API call when you will always get a single response, and I want to know what would be the best approach.
Initially I've used the plain subscribe
, then I've learnt about memory leaks.
Later I've used either _service.pipe(take(1)).subscribe
or packages like untilDestroyed
that cancel the subscription upon component destruction.
Assigning the subscription to a variable and unsubscribing on ngOnDestroy
is the least desirable option for me.
Another option is converting the observable to a promise and then use a bit of syntactic sugar with async/await
.
Since I am periodically refactoring my code, the question remains. What is the best approach and why?