【发布时间】:2020-03-30 21:38:10
【问题描述】:
我一直在尝试找到一种方法,将来自前一个 flatMap 运算符的 map 函数中的多个操作链接起来。
当前功能:
flatMap(
someResponse=> combineLatest([
this.locator.function(someResponse, variable2),
this.function1(someResponse),
])
),
map(([response1, response2]) => this.function3(response1, response2));
所需的新功能是这样的:
flatMap(someResponse =>
combineLatest([
this.locator.function(someResponse, variable2),
this.function1(someResponse),
this.locator.function2(someResponse, variable3)
])
),
map(([response1, response2, response3]) => {
this.function3(response1, response2);
this.function4(response3);
})
我尝试了另一张地图,但它得到了之前的地图响应,而不是 flatMap。我对这些运营商的工作方式并不完全有信心。比如switchMap、contactMap、mergeMap等。我没找到办法。
我在 flatMap 中链接并组合了 3 个函数,然后我想使用响应来执行一些其他操作。
我该怎么做?
【问题讨论】:
-
所以你想运行一个 observable,然后并行链接 3 个 observable,然后在 3 个 observable 都返回值时调用一些同步函数?
-
没错。或者如果有更好的方法而不是这种结构。
-
在您的示例中,您想在
map中做什么? -
如果最后一次调用是同步的,那么订阅
flatMap而不是使用map不是很优雅吗? -
它们都在映射来自后端的一些响应。所有函数都返回数组。
标签: angular typescript rxjs