How to return value inside subscribe Angular 4 : <p>I'm new to observables in angular. I have a problem wanting to return a value inside a subscribe method. I have
the following method (<code>getFirebaseData(idForm:string):observable &lt;any[]&gt;</code>):</p>

<pre><code>getTotalQuestions(idForm:string){
let totalQuestions:number;
this.getFirebaseData(idForm+"/Metadatos")
.subscribe(items =&gt; 
  {
    items.map(item =&gt; {
      totalQuestions=item.Total;
      console.log(totalQuestions);
    });
  }
);
console.log(totalQuestions);
return totalQuestions;
}
</code></pre>

<p>the firts <code>console.log(totalQuestions)</code> print <strong>4</strong> but the second <code>console.log(totalQuestions)</code> print <strong>undefined</strong>. 
I understand that subscribe is an asynchronous operation and that for that reason the second <code>console.log(totalQuestions)</code> ("In order to write the code") print undefined, but I can not find the way to return the variable after the subscribe method has been completed. Now, if I change the subscribe to map:</p>

<pre><code>getTotalQuestions(idForm:string){
let totalQuestions:number;
this.getFirebaseData(idForm+"/Metadatos")
.subscribe(items =&gt; 
  {
    items.map(item =&gt; {
      totalQuestions=item.Total;
      console.log(totalQuestions);
    });
  }
);
console.log(totalQuestions);
return totalQuestions;
}
</code></pre>

<p>the firts <code>console.log(totalQuestions)</code> does not print anything and the second <code>console.log(totalQuestions)</code> print undefined. It's something that I do not understand because it happens.</p>

<p>I hope you can help me clarify the concept that I do not understand. Thanks!</p>
