Sander
Sander

Reputation: 103

Chaining observables with RxJS in Angular2

I have 2 API calls -- the second call uses something the first call returns. With promises this was easy:

myService.findAll()

     // First call
    .then(response => {
        return myService.findSpecific(response.something);
    })

    .then(response => {
        // result from second API call
    });

How would I do this using observables?

Upvotes: 7

Views: 2041

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202306

You can leverage the flatMap operator this way:

myService.findAll()
  // First call
  .flatMap(response => {
    return myService.findSpecific(response.something);
  }).subscribe(response => {
    // result from second API call
  });

Upvotes: 8

Related Questions