title: Angular 7: Await function in interceptor
body: <p>I have built an error interceptor in my first Angular application which is all new for me. The interceptor tries to refresh a Firebase authorization token when a <code>401</code> response code occures. Therefore I have written the following code:</p>
  
  <pre><code>@Injectable()
  export class ErrorInterceptor implements HttpInterceptor {
  constructor(private authService: AuthService, private alertService: AlertService) { }
  
  intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
      return next.handle(request).pipe(
          catchError(err =&gt; {
              if (err.status === 401) {
                  let user = localStorage.getItem('currentUser');
                  if (!user) {
                      this.logout(false);
                      return throwError(err.error);
                  }
                  let currentUser = JSON.parse(user);
                  if (!currentUser || !currentUser.stsTokenManager || !currentUser.stsTokenManager.accessToken) {
                      this.logout(false);
                      return throwError(err.error);
                  }
                  const reference = this;
                  this.authService.getToken(currentUser, true).then(t =&gt; {
                      // How do I await and return this properly?
                      return reference.updateTokenAndRetry(request, next, currentUser, t);
                  }); // Get token and refresh
              }
              this.alertService.showAlert({
                  text: 'Fout tijdens het verzenden van het verzoek',
              });
              return throwError(err.error);
          })
      );
  }
  
  updateTokenAndRetry(request: HttpRequest&lt;any&gt;, next: HttpHandler, currentUser: any, token: string): Observable&lt;HttpEvent&lt;any&gt;&gt; {
      // Update local stored user
      currentUser.stsTokenManager.accessToken = token;
      localStorage.setItem('currentUser', JSON.stringify(currentUser));
  
      // Add the new token to the request
      request = request.clone({
          setHeaders: {
              Authorization: token,
          },
      });
  
      return next.handle(request);
  }
  </code></pre>
  
  <p>The token gets refreshed fine. However the network call is not being executed after the refresh, which <code>reference.updateTokenAndRetry(request, next, currentUser, t);</code> should do.</p>
  
  <p>I assume the reason for this, is that <code>this.authService.getToken(currentUser, true)</code> returns a <code>Promise</code> (this is the Firebase plugin and can't be changed). I want to return <code>return reference.updateTokenAndRetry(request, next, currentUser, t);</code> but this is not possible since it's in an async function block.</p>
  
  <p>How can I await or return the next network call? I can't make the <code>intercept</code> function <code>async</code>. I am pretty stuck at this point.</p>