javascript - 从 Observable 链返回所有结果
问题描述
在我的 Angular 应用程序中,我试图实现以下目标
- 执行
Observable
A - 当 A 完成时
Observable
,使用 A 的结果执行 B - 当 B 完成时,返回A 和 B的结果
如果没有第三点,我想这会很简单,我会做这样的事情:
this.myService.getData()
.pipe(
flatMap(firstResult => this.myService.getMoreData(firstResult.someField))
).subscribe(secondResult => {
// process the data
});
问题是这种方式我最终只能访问secondResult
,但我也需要firstResult
。
我不是 RxJS observables 方面的专家,所以我真的很感激任何关于如何解决这个问题的建议。
解决方案
您可以将第二个结果映射到两个结果的数组。
this.myService.getData()
.pipe(
concatMap(firstResult => this.myService.getMoreData(firstResult.someField).pipe(
map(secondResult => [firstResult, secondResult]),
))
).subscribe(([first, second]) => ...);
推荐阅读
- .htaccess - 在 htaccess 中丢弃 RewriteRule
- django-rest-framework - 嵌套写入序列化程序验证日期为空
- javascript - 我如何知道我在 react-navigation 5 中的当前路线?
- java - Enum 的匿名实现不能用参数引用 Enum 的私有静态 toString
- node.js - 默认情况下,pm2 会在重启后自动重启应用程序吗?
- laravel - 安装包时出错(安装失败,正在将 ./composer.json 还原为其原始内容。)(Laravel 管理包 - Voyager)
- django - Django:重新编码请求并调用另一个视图
- sql - SQL COUNT 具有相同或更小的值的所有行
- c# - 资源服务器如何知道使用哪种算法来验证 JWT?
- reactjs - React 和 Material-UI:MuiDialog-root 在对话框关闭后卡住