title: RxJs: conditional switchMap if emitted value is empty
body: <p>I have this code :</p>
  
  <pre><code>const id = 1; // id = 2
  
  of([{id: 1, name: 'abc'}]).pipe(
              map(items =&gt; items.find(item =&gt; item.id === id)),
              switchMap(item =&gt; item ? of(item) : this.makeHttpRequest(id))
          );
  
  makeHttpRequest(id: number): Observable&lt;IdNamePair&gt;{}
  </code></pre>
  
  <p>in short,</p>
  
  <ul>
  <li><p>if object with specific id is found in array, return observable
  of({id: 1, name: 'abc'})</p></li>
  <li><p>if object not found in array, return observable returned by<br>
  this.makeHttpRequest(id) method</p></li>
  </ul>
  
  <p>I want to eliminate the of(item) operator.</p>
  
  <p>This code works but is there a way without creating a new observable and reusing the one created by "map()" ?</p>
  
  <p>edit: </p>
  
  <pre><code>export class MyService {
      private _list = new BehaviorSubject([{id: 1, name: 'abc'}]);
      list = this._list.asObservable();
  
      findOne(id: number) {
          this.list.pipe(
                      map(items =&gt; items.find(item =&gt; item.id === id)),
                      switchMap(item =&gt; item ? of(item) : this.makeHttpRequest(id))
                  );
      }
  }
  </code></pre>
  
  <p>in Angular component :</p>
  
  <pre><code>export class MyComponent {
  
      item$: Observable&lt;IdNamePair&gt;;
  
      constructor(private service: MyService) {}
  
      ngOnInit() {
              this.item$ = this.service.findOne(1); // or (2), id comes from router
      }
  }
  </code></pre>