title: How to properly unsubscribe from an Observable Angular 7?
body: <p>I am currently looking at making sure my application has no memory leaks. I am following <a href="https://blog.angularindepth.com/angular-question-rxjs-subscribe-vs-async-pipe-in-component-templates-c956c8c0c794" rel="nofollow noreferrer">this</a> tutorial where he shows how to change from using <code>subscribe()</code> to <code>async</code> as well as showing how to explicitly unsubscribe during <code>ngOnDestroy</code>.</p>
  
  <p>I am struggling to test whether my implementation worked and I wanted to know if my implementation of it in my project is actually unsubscribing.</p>
  
  <p>Here is what is in the tutorial in his component: </p>
  
  <pre><code>@Component({
    /* ... */
    template: `
      &lt;ul *ngIf="todos.length &gt; 0"&gt;
        &lt;li *ngFor="let todo of todos"&gt;{{todo.name}}&lt;/li&gt;
      &lt;/ul&gt;   
    `
  })
  export class TodosComponent implements OnInit, OnDestroy {
    private unsubscribe$ = new Subject&lt;void&gt;();
  
    todos: Todo[];
  
    constructor(private store: Store&lt;State&gt;) {}
  
    ngOnInit() {
      this.store
        .pipe(select(selectTodos), takeUntil(this.unsubscribe$)) // unsubscribe to prevent memory leak
        .subscribe(todos =&gt; this.todos = todos);            // unwrap observable
    }
  
    ngOnDestroy(): void {
      this.unsubscribe$.next();
      this.unsubscribe$.complete();
    }
  }
  </code></pre>
  
  <p>My ngOnDestroy looks exactly the same as his, but this is what my call to the service looks like:</p>
  
  <pre><code>this.insightsService.getLatestsInsights(5)
        .pipe(takeUntil(this.unsubscribe$))
        .subscribe(z =&gt; {
          if (z != null) {
            this.latestInsights = z;
            this.visibleSlides = z.slice(0, this.amountOfSlidesToShow);
          }
        })
  </code></pre>
  
  <p>So the service works but I just want to know if my implementation is correct? Wasn't sure what the select function was as well or if that is only applicable with what he is using. Let me know if I can provide info.</p>