title: HTTP Interceptor not waiting for token to be refreshed before sending next request
body: <p>I'm using Ionic 3 &amp; Angular 5. </p>
  
  <p>I am having trouble implementing a HTTP interceptor that refreshes my token if its expiring soon and sends the next request with the new token. The problem is that its not waiting for the refresh token to be updated before making the request. How can I fix this?</p>
  
  <p>AuthService</p>
  
  <pre><code>refreshToken(token?: string) {
  return this.http.get(`${this.API_URL}/refresh`)
    .pipe(catchError(this.handleServerError));
  }
  </code></pre>
  
  <p>TokenInterceptor</p>
  
  <pre><code>intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
  
      // If the next request is to my refresh endpoint allow it through
      if (URL == `${this.globals.APIURL}/refresh`) {
          return request.clone({
                  setHeaders: {
                    Authorization: `Bearer ${token}`,
                    'Accept': 'application/json',
                  }
                });
      }
      // Else check if token is expiring soon and refresh it and send the new token in the request
      else {
        const expirationLeft = this.authService.getTokenExpiration();
        if (token &amp;&amp; expirationLeft &lt; (this.offsetSeconds * 1000) &amp;&amp; !this.isUpdating) {
          this.isUpdating = true;
          try {
            this.authService.refreshToken(token)
              .subscribe(data =&gt;
              {
                token = data.token;
                this.authService.storeToken(token);
                this.authService.authenticationNotifier().next(true);
                const clone = request.clone({
                  setHeaders: {
                    Authorization: `Bearer ${token}`,
                    'Accept': 'application/json',
                  }
                });
                this.isUpdating = false;
                return next.handle(clone);
  
              },
              (err) =&gt;
              {
                this.authService.logout();
                this.isUpdating = false;
              });
          }
          catch (e) {
  
          }
      }
      return request.clone({
          setHeaders: {
            Authorization: `Bearer ${token}`,
            'Accept': 'application/json',
          }
        });
  }
  </code></pre>