title: HttpInterceptor calling another service inside an interceptor and waiting for response
body: <p>I have to call another service in an interceptor if a certain condition is met and I want the original service to wait until the new service will complete. My first solution was using tap:</p>
  
  <pre><code>intercept(req: HttpRequest&lt;any&gt;, options: any, next: HttpServiceHandler)
    : Observable &lt;HttpEvent&lt;any&gt;&gt; {
      return next.handle(req, options).pipe(
        tap((event: HttpEvent&lt;any&gt;) =&gt; {
          if (event instanceof HttpResponse &amp;&amp; event.ok &amp;&amp; event.body) {
            const productsToGet = [...]
            if (* condition *) {
              const resourceService = this.injector.get(ResourceService);
              return resourceService.getProducts(productsToGet);
            }
          }
        })
      );
    }
  </code></pre>
  
  <p>But it obviously didn't work like I wanted to because original method finished before getProducts was completed. Then I used solution from this <a href="https://stackoverflow.com/questions/47656992/httpinterceptor-change-response-body-based-on-value-from-other-observable">thread</a> with switchMap:</p>
  
  <pre><code>return next.handle(req, options).pipe(
    filter((event: HttpEvent&lt;any&gt;) =&gt; (event instanceof HttpResponse &amp;&amp; event.ok &amp;&amp; event.body)),
    switchMap((event: HttpResponse&lt;any&gt;) =&gt; {
      const productsToGet = [...]
      if (* condition *) {
        const resourceService = this.injector.get(ResourceService);
        return resourceService.getProducts(productsToGet).pipe(
          mapTo(event)
        )
      } else {
        return of(event);
      }
    })
  );
  </code></pre>
  
  <p>But now I'm getting an error: </p>
  
  <blockquote>
    <p>Uncaught (in promise) EmptyError: no elements in sequence.</p>
  </blockquote>
  
  <p>Can you tell me if my solution is valid and what should I do to make it work?</p>
  
  <p>Full error stack:</p>
  
  <pre><code>&gt; Error: Uncaught (in promise): EmptyError: no elements in sequence
      at resolvePromise (zone.js:852)
      at resolvePromise (zone.js:809)
      at zone.js:913
      at ZoneDelegate.push.../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
      at Object.onInvokeTask (core.js:26754)
      at ZoneDelegate.push.../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
      at Zone.push.../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
      at drainMicroTaskQueue (zone.js:601)
      at ZoneTask.push.../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:502)
      at invokeTask (zone.js:1693)
  </code></pre>