Andrew Howard
Andrew Howard

Reputation: 3082

Rxjs subscription in constructor unwise

I was told in a recent job interview that by placing a subscription in the constructor (for example to get a query param) was unwise as it can be intermittent as to when that value is available. Is that correct? I’ve personally done it in all my projects so far and have never had an issue with that.

Upvotes: 0

Views: 493

Answers (1)

anaval
anaval

Reputation: 1138

was unwise as it can be intermittent as to when that value is available. Is that correct?

the real is answer is it depends from company-to-company. a good practice in company X might be a bad practice in company Y(and vice versa)

most of the time ngOnInit is the correct place but that doesnt really apply to services. it doesnt really matter where you want to call .subscribe() as long as you are managing your subscriptions and able to .unsubscribe() to them when you need to. I myself avoid calling .subscribe() to my constructors, but that doesnt really warrant saying "your way is wrong".

Who decides which is wrong and right? The most important thing is consistency, if majority of you team codes it in the constructor, then you should code it in the constructor. if majority of your team says its wrong, then you follow them and avoid calling it in your constructor.

Upvotes: 1

Related Questions