在rxjs可观察链中传递复合数据 [英] 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
})
现在,我知道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屋!