最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing composite data in rxjs observable chains - Stack Overflow

programmeradmin3浏览0评论

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 es 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

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 es 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

Share Improve this question asked Apr 24, 2017 at 22:20 Siegmund NagelSiegmund Nagel 1,3812 gold badges11 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 18

You can use a map operator to bine 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 }) =>
    ...
发布评论

评论列表(0)

  1. 暂无评论