title: Typescript Angular HTTP request get only last result
body: <p>What is the &quot;best&quot; (common) way to make sure that my Angular HTTP request only returns the newest response data. (I am using Angulars HttpClient)</p>
  <p>Lets say the user submits a new request before the response of the previous request is returned. My backend needs more time to process the first request so the second request returns before the first one -&gt; the view get's updated and later the first request returns. Now the view gets updated with old data. Thats obviously not what I want.</p>
  <p>I found this solution to cancel the request but is that really the only way? Are there any build in functionalities to achive that?</p>
  <pre><code>if ( this.subscription ) {
     this.subscription.unsubscribe();
  }
  this.subscription = this.http.get( 'myAPI' )
   .subscribe(resp =&gt; {
    // handle result ...
  });
  </code></pre>
  <p>I know there is the <strong>switchMap()</strong> operator which only returns the latest data but as far as I know you can only use it inside of an observable. In my case I am not observing any input changes. I simply call a function on form submit via (ngSubmit)=&quot;doRequest()&quot; directive in HTML-Template.</p>
  <pre><code>// get called on submit form
  doRequest() {
   this.http.get('myAPI')
    .subscribe(resp =&gt; {
      // handle result ...
    });
  }
  </code></pre>
  <p>Is there a way to use the switchMap operator or do you have any other solutions? Thanks for taking your time :)</p>
  <p>PS: Of course in my real project I have an api service class with different functions doing the requests and just returning http response observables. So I subscribe to such an observable in doRequest() function.</p>