Search code examples
angularrxjsrxjs6

RXJS - multiple consecutive http requests


source<http>
  .pipe(
    switchMap(d => this.http.get(d))
      .pipe(
        switchMap(j => this.http.get(j))
      )
  )
  .subscribe()

Hello, I need to make 3 http requests consecutively where each call contains data for the next call.. are nested switch maps the best practice for this case?


Solution

  • You don't need to nest them. You can simply chain them:

    source<http>
      .pipe(
        switchMap(d => this.http.get(d)),
        switchMap(j => this.http.get(j))
      )
      .subscribe()
    

    Apart from that, using multiple switchMap is the way to go.