title: Angular 2+ wait for subscribe to finish to update/access variable
body: <p>I am having an issue with my variables being undefined. I am certain this is because the observable hasn't finished. Here is the part of my code in my .ts file that is causing the issue. (I'm placing the minimum code required to understand the issue. Also <code>myFunction</code> gets called from a click event in the HTML).</p>
  <pre><code>export class myClass {
    myVariable: any;
  
    myFunction() {
      this.myService.getApi().subscribe(data =&gt; {
        this.myVariable = data;
      });
  
      console.log(myVariable) --&gt; undefined
    }
  }
  </code></pre>
  <p>So this piece of code calls a function in my service that returns some data from an API. The issue is that when I try to access the variable <code>myVariable</code> right outside of the subscribe function it returns undefined. I'm sure this is because the subscribe hasn't finished before I try to access <code>myVariable</code></p>
  <p>Is there a way to wait for the subscribe to finish before I try to access <code>myVariable</code>?</p>