title: Angular: have side effects and return Observable from a method
body: <p>I have this function:</p>
  
  <pre><code>  /**
     * Attempt login with provided credentials
     * @returns {Observable&lt;number&gt;} the status code of HTTP response
     */
    login(username: string, password: string): Observable&lt;number&gt; {
      let body = new URLSearchParams();
      body.append('grant_type', 'password');
      body.append('username', username);
      body.append('password', password);
  
      return this.http.post(Constants.LOGIN_URL, body, null)
        .do(res =&gt; {
          if (res.status == 200) {
            this.authTokenService.setAuthToken(res.json().auth_token);
          }
        })
        .map(res =&gt; res.status)
        .catch(err =&gt; { return Observable.throw(err.status) });
    }
  </code></pre>
  
  <p>This function attempts to perform login and returns <code>Observable&lt;number&gt;</code> that the caller can use in order to be notified of the HTTP code of the response. </p>
  
  <p>This seems to work, but I have one problem: if the caller just calls the function without subscribing to the returned <code>Observable</code>, <code>do</code> function is not called and the received auth token is not being saved.</p>
  
  <p>Documentation of <code>do</code> states:</p>
  
  <blockquote>
    <p>Note: this is different to a subscribe on the Observable. If the
    Observable returned by do is not subscribed, the side effects
    specified by the Observer will never happen. do therefore simply spies
    on existing execution, it does not trigger an execution to happen like
    subscribe does.</p>
  </blockquote>
  
  <p>What I'd like to achieve, is to have this side effect regardless of whether the caller of <code>login()</code> subscribed or not. What should I do?</p>