Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 341
Sander Avatar asked Apr 25 '16 10:04

Sander


1 Answers

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
  });
like image 155
Thierry Templier Avatar answered Oct 21 '22 20:10

Thierry Templier