简体   繁体   中英

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?

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
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM