DEV Community

Discussion on: The Ultimate Answer To The Very Common Angular Question: subscribe() vs | async Pipe

Collapse
 
chiangs profile image
Stephen Chiang

Would i need to manually unsubscribe in OnDestroy if I did it this way (not @Effect) as you have described?

Thread Thread
 
tomastrajan profile image
Tomas Trajan 🇨🇭

Yes, whenever you do explicit .subscribe() call, you are automatically responsible to call .unsubscribe(). Even better would be to use .pipe(takeUntil(onDestroy$)) which is declarative ;)

Thread Thread
 
chiangs profile image
Stephen Chiang • Edited

awesome, thanks!

so then I would also need to do this, right?

ngOnDestroy() {
    this.onDestroy$.next(true);
    this.onDestroy$.unsubscribe();
  }
Thread Thread
 
tomastrajan profile image
Tomas Trajan 🇨🇭 • Edited
ngOnInit() {
   this.source$.pipe(takeUntil(onDestroy$)).subscribe()
}

ngOnDestroy() {
    this.onDestroy$.next(true);
    this.onDestroy$.complete()
}

CHeers!