title: calling a method inside a subscribe function is not waiting to get the result in angular 4
body: <p>I am very new in angular 4. I am fetching a result set using a subscribe method and inside that I need to call another method to manipulate the first result set. But before getting the second result set, the process is getting completed.</p>
  
  <p>My code in component.ts is</p>
  
  <pre><code>workItems: IGeneratedTasks[];
  this._taskManagementService.getAllWorkItems()
    .subscribe(workItems =&gt; {
      this.workItems = [];
      if (workItems != null)
        workItems.forEach(result =&gt; {            
  
          this._taskManagementService.hasAttachment(result.id).subscribe(count =&gt; {
            console.log(count);
            if (count &gt; 0)
              result.hasAttachment = true;  //hasAttachment is a property in IGeneratedTasks
          });         
  
  
          console.log(result.hasAttachment);
  
  
        });
      this.workItems = workItems;
  
    },
    error =&gt; this.errorMessage);
  </code></pre>
  
  <p>my service.ts is</p>
  
  <pre><code>getAllWorkItems(): Observable&lt;IGeneratedTasks[]&gt; {       
      return this._http.get(this._workItemUrl)
          .map(res =&gt; res as IGeneratedTasks[] || [])
          .map(tasks =&gt; tasks.map(this.generatedTasksService.toGeneratedTasks))
          .catch(this.handleError);
  }
  
   hasAttachment(id:Number): Observable&lt;number&gt; {       
      let myHeaders = new HttpHeaders();
      myHeaders.append('Content-Type', 'application/json');
      let myParams = new HttpParams();
      myParams = myParams.append('workItemId',id.toString());
  
      return this._http.get(this._hasAttachmentUrl,{ headers: myHeaders, params: myParams })
          .map(res =&gt; res)           
          .catch(this.handleError);
  }
  </code></pre>
  
  <p>Am I calling the methods in the right way? </p>
  
  <p><strong>Small change after the suggestions</strong>,</p>
  
  <pre><code>this._taskManagementService.getAllWorkItems()
  
    .subscribe(workItems =&gt; {
      this.workItems = [];
      let newWI: IGeneratedTasks[] = [];
      if (workItems != null) {
        workItems.forEach(result =&gt; {
          result.isOverDue = result.duedate != null &amp;&amp; result.duedate &lt; (new Date()) &amp;&amp; result.overallstatus != "4";
          workItems.forEach(result =&gt; {
            this._taskManagementService.hasAttachment(result.id).subscribe(count =&gt; {
              if (count &gt; 0) {
                result.hasAttachment = true;
              }
              newWI.push(result);
            });
  
          })
        });
  
      }
   this.workItems = newWI;
  
      },
    error =&gt; this.errorMessage);
  </code></pre>
  
  <p>I have a similar call like hasAttachment</p>
  
  <pre><code>  this._taskManagementService.hasNote(result.id).subscribe(count =&gt; {
              if (count &gt; 0) {
                result.hasNote = true;
              }                
            });
  </code></pre>
  
  <p>Where do I add this, so that the final dataset contains both the hasAttachment and hasNote.</p>