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

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

问题描述

我有一段代码在这样的链中调用可观察对象:

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将使我可以将上一个可观察到的结果传递给下一个可观察到的结果.但是,我需要既执行此操作,又将结果传递给第一个.假设我对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投影函数接收到的参数与可观察对象的结果组合在一起:

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天全站免登陆