简体   繁体   中英

Passing composite data in rxjs observable chains

I have a block of code where I'm calling observables in a chain like so:

getData().flatMap(results => {
   return callNextDataMethod(results);
}
.flatMap(results2 => {
   // next operation and so forth
})

Now, I understand that flatMap will allow me to pass the results of the previous observable to the next one. However what I need is to both do that as well as pass the results on the first. Let's assume that I do some cleanup, validation, etc on the data that comes back in getData and I want that passed to all flatMap calls down the chain. Is there an operator in rxjs that will do this for me?

Thanks

You can use a map operator to combine the argument received by the flatMap projection function with the observable's result:

getData()
  .flatMap(data =>
    getMoreData(data).map(moreData => ({ data, moreData }))
  )
  .flatMap(({ data, moreData }) =>
    getEvenMoreData(moreData).map(evenMoreData => ({ data, moreData, evenMoreData }))
  )
  .flatMap(({ data, moreData, evenMoreData }) =>
    ...

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