在 rxjs 可观察链中传递复合数据 [英] Passing composite data in rxjs observable chains

查看:21
本文介绍了在 rxjs 可观察链中传递复合数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码块,我在这样的链中调用 observables:

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

现在,我知道 flatMap 将允许我将前一个 observable 的结果传递给下一个.但是,我需要的是既要做到这一点,又要在第一个上传递结果.假设我对 getData 中返回的数据进行了一些清理、验证等操作,并且我希望将其传递给链中的所有 flatMap 调用.rxjs 中是否有运算符可以为我执行此操作?

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?

谢谢

推荐答案

您可以使用 map 运算符将 flatMap 投影函数接收到的参数与 observable 的结果组合起来:

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 }) =>
    ...

这篇关于在 rxjs 可观察链中传递复合数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆