title: Angular, ngrx/store, store select and async call in a single sequence
body: <p>Does anybody know if there is a chance to get a value from store and then call angular http service? See example below. Thanks in advance.</p>
  
  <p>I have a service with async method. In this method I call angular http service, but before I call angular http service I want to retrieve some value from the store (oauth access token). The thing is that I store oauth token in the store, and I have to add token to the request. If server responses with 401 error I refresh token and repeat request or requests with new token.</p>
  
  <pre><code>@Injectable()
  export class Interceptor {
     public get(request: Request): Observable&lt;any&gt; {
        return this.store.select((s) =&gt; s.session.accessToken)
           // tried .switchMap, it wasn't working for multiple requests
           // .flatMap also is not working
           // commented below is working but I don't think that it is right solution
           // return Observable.of(request)
           // .withLatestFrom(this.store.select((s) =&gt; {
           //    return s.session.accessToken
           // }))
           // .flatMap(([config, accessToken]) =&gt; { ... })
           .flatMap((accessToken) =&gt; {
              request.headers.set("Authorization", `Bearer ${accessToken}`);
  
              return this.http.request(request);
           })
           .map((response) =&gt; {
              return response.json();
           })
           .catch((response) =&gt; {
              return this.handleError(response, request);
           });
     }
  
     // handle errors
     private handleError(response: Response, request: Request): Observable&lt;Response&gt; {
         // handle outdated api tokens
         if (response.status === HttpStatusesEnum.Unauthorized) {
             const deferredRequest = new DeferredRequestModel({ request });
  
             // appends HTTP request configuration object with deferred response
             // attached to buffer
             this.buffer(deferredRequest );
  
             return deferredRequest.observable;
         }
  
         // handle server error
         if (response.status === HttpStatusesEnum.InternalServerError) {
             ...
         }
  
         ...
  
         return Observable.throw(response);
     }
  }
  
  export class DeferredRequestModel {
      public observable: AsyncSubject&lt;Response&gt;;
      public request: Request;
  
      constructor(obj?: Partial&lt;DeferredRequestModel&gt;) {
          this.observable = new AsyncSubject&lt;Response&gt;();
          this.request = obj &amp;&amp; obj.request || null;
      }
  
      public reject(error?: any): void {
          this.end({ isValue: false, value: error });
      }
  
      public resolve(value?: any): void {
          this.end({ isValue: true, value });
      }
  
      private end(result: any): void {
          if (this.observable.isStopped) {
              console.warn("Deferred already resloved/rejected.");
              return;
          }
  
          if (result.isValue) {
              this.observable.next(result.value);
              this.observable.complete();
          } else {
              this.observable.error(result.error);
          }
      }
  }
  </code></pre>
  
  <p>Then when I call some service, it will send request through interceptor.</p>
  
  <pre><code>export class SomeEffect {
     constructor(private someService: SomeService) {}
  
     @Effect()
     public someMethod(): Observable&lt;Action&gt; {
        return this.actions$.ofType(SOME_ACTION_GET)
           .map((action: SomeActions.Get) =&gt; action.payload)
           .switchMap((someObj) =&gt; {
              // someMethod call interceptor inside someService, it won't call angular 
              // http service directly
              return this.someService.someMethod(someObj);
           })
           .map((someResult) =&gt; {
              return new SomeActions.GetSuccess(someResult);
           });
     }
  }
  </code></pre>
  
  <p>I've tried to use switchMap in the interceptor, but it won't work if the interceptor is called more than a single time.</p>