Reputation: 4818
I'd like to know if this code has any sense or not:
let subs : Subscription;
// getObjectInfo retrieves object information from a MySQL DB
subs = this.ObjectService.getObjectInfo(item.id_obj)
.subscribe(
(retObject : any) => {
// Process object here
},
(error) => {
// Error in API call
},
() => {
// IS THIS REDUNDANT OR UNNECESSARY?
subs.unsubscribe();
});
What I pretend is to make sure that the subscription is finished as soon as I get the info from the DB and I process it, but I don't know if this is a good way or if it's unnecessary...
The reason I'm asking this is because I see duplicate API calls sometimes and I wonder if they are because I didn't finish the subscriptions in a proper way or something like that.
Thanks!
Upvotes: 1
Views: 81
Reputation: 7925
You can use take
or first
:
this.ObjectService.getObjectInfo(item.id_obj)
.take(1) // or first()
.subscribe(...)
So it will automatically unsubscribe after the first emission
Upvotes: 1