Hi I am trying to check when a token is about to expire and reload it with the access token.
The main problem is that to reload it I have to make a http call to get the new token and then I will re save it in the session storage.
The main problem is that because the http is async it reloads the token, but because I dont know how to wait until this call is finish to retry the original call with the new token it fail.

This is the code:

AuthService:
This is the service that is being called to update the token

```js
async reloadIdTokenFromAccess(accessToken : string | null) {
  console.log("Performing call to reload token");
  const resp = await this.httpClient.post<IdTokenDTO>(`${environment.backendUrl}/auth/tokenReload`,accessToken).toPromise();
  this.setIdToken(resp.idToken);
  this.setExpireAt(resp.expireAt);
  console.log("Data update in session storage")
}

```

interceptor

```js
@Injectable()
export class HttpTokenInterceptor implements HttpInterceptor {

  constructor(private authServ : AuthService) {}

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {

    let token: string | null = this.authServ.getIdToken();
    let expiration: number | null = this.authServ.getExpireAt();
    let reloaded : boolean = false;

    var currentTime = new Date().getTime();

    if(expiration != null && !request.url.includes("tokenReload") && !request.url.includes("login")){
      if(expiration <= currentTime + 60000 && this.authServ.getAccessToken() != null){
        reloaded = true;
        console.log("Token expiration Time near to expire, reloading token")
        this.authServ.reloadIdTokenFromAccess(this.authServ.getAccessToken());
        token = this.authServ.getIdToken();
        console.log("Finish all full reloading")
      }
    }

    if(reloaded){
      console.log("Starting the request with the new token")
    }

    let originalReq = request;

    if(token){
      request = originalReq.clone({
        setHeaders: {
          authorization: `Bearer ${ token }`,
          'Content-Type': 'application/json'
        }
      });
    }

    return next.handle(request);
  }
}

```

If I check the logs I see the following:
[![enter image description here](https://i.stack.imgur.com/JXE08.png)](https://i.stack.imgur.com/JXE08.png)

As you can check the request is being performen before the token is reloaded.
How can i force this request to be finish before continue with the method?

Just something similar like in java a CompletableFuture with the get() method to block the code until it finish

I also have test things like this without success

```js
async reloadIdTokenFromAccess(accessToken : string | null) {
  console.log("Performing call to reload token");
  await this.httpClient.post<IdTokenDTO>(`${environment.backendUrl}/auth/tokenReload`,accessToken).subscribe( (resp : IdTokenDTO) => {
    this.setIdToken(resp.idToken);
    this.setExpireAt(resp.expireAt);
    console.log("updates token in session storage")
  })
}

```

```js
import { Injectable } from '@angular/core';
import {HttpRequest,HttpHandler,HttpEvent,HttpInterceptor} from '@angular/common/http';
import { from, Observable, Subscription } from 'rxjs';
import { AuthService } from '../services/auth.service';

@Injectable()
export class HttpTokenInterceptor implements HttpInterceptor {

  constructor(private authServ : AuthService) {}

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    return from(this.handle(request, next));
  }

  async handle(request: HttpRequest<any>, next: HttpHandler) {

    const expiration: number | null = this.authServ.getExpireAt();
    const currentTime = new Date().getTime();
    let reloaded: boolean = false;

    if (expiration != null && !request.url.includes('tokenReload') &&!request.url.includes('login')) {
      if (expiration <= currentTime + 60000 && this.authServ.getAccessToken() != null) {
        reloaded = true;
        console.log('Token expiration Time near to expire, reloading token');
        await this.authServ.reloadIdTokenFromAccess(this.authServ.getAccessToken());
        console.log('Finish of call reaload token');
      }
    }

    const token = this.authServ.getIdToken();

    if (reloaded){ 
      console.log('Starting the request with the new token');
    }

    const originalReq = request;

    if (token) {
      request = originalReq.clone({
        setHeaders: {
          authorization: `Bearer ${token}`,
          'Content-Type': 'application/json',
        },
      });
    }
    return next.handle(request).toPromise();

  }
}

```

I am desesperate. dont know what to do (maybe continue studing courses and tutorials but i have dedlines) :(

Thanks in advance.