![](/img/trans.png)
[英]How to combine 2 or more observables so that you know which emitted?
[英]How to "combine" 3 or more Observables?
我有来自 3 个不同服务的这 3 个 Observables(3 个 API 调用):
this.gs.getLocationName().subscribe((loc) => this.locationName = loc);
this.gs.getLocationInfo(this.locationName).subscribe((data) => {
this.lat = data.results.geometry.location.lat;
this.lon = data.results.geometry.location.lng;
});
this.ws.getWeatherByCoordinates(this.lat, this.lon).subscribe((data) => ...);
正如你所看到的,它依赖于之前的 Observable,所以我想一个一个地运行它们。
例如,我知道如何将 2 Observable 与 pipe 和 mergeMap “组合”,但是 3 有问题
我的解决方案是这样的:
this.gs
.getLocationName()
.pipe(
tap((loc) => {
this.locationName = loc;
}),
mergeMap((loc) => {
return this.gs.getLocationInfo(this.locationName);
})
)
.pipe(
tap((data) => {
this.lat = data.results[0].geometry.location.lat;
this.lon = data.results[0].geometry.location.lng;
})
)
.subscribe((data) => {
this.ws.getWeatherByCoordinates(this.lat, this.lon).subscribe((data) => ...);
});
虽然我不确定在订阅中订阅是否是一种好习惯,但它有效吗?
所以我的下一个解决方案是:
this.gs
.getLocationName()
.pipe(
tap((loc) => {
this.locationName = loc;
}),
mergeMap((loc) => {
return this.gs.getLocationInfo(this.locationName);
})
)
.pipe(
tap((data) => {
this.lat = data.results[0].geometry.location.lat;
this.lon = data.results[0].geometry.location.lng;
}),
concatMap((data) => {
return this.ws.getWeatherByCoordinates(this.lat, this.lon);
})
)
.subscribe((data: WeatherModel) => {
...
});
这也有效,但我也不确定我是否做得正确。 不确定 concatMap 是否是 goo 的方法,但它至少对我有用。
有什么技巧可以提高我的代码质量吗?
你做得很好,继续mergeMap
。 你可以mergeMap
多个Observable
this.gs.getLocationName().pipe(
tap(loc => this.locationName = loc),
mergeMap(locationName => this.gs.getLocationInfo(locationName)),
tap(data => {
this.lat = data.results.geometry.location.lat;
this.lon = data.results.geometry.location.lng;
}),
mergeMap(data => this.ws.getWeatherByCoordinates(this.lat, this.lon))
).subscribe((data) => {
...
});
此外,如果您不使用属性locationName
、 lat
和lon
,您可以将上述内容简化为
this.gs.getLocationName().pipe(
mergeMap(loc => this.gs.getLocationInfo(locationName)),
map(data => ({
lat : data.results.geometry.location.lat,
lon : data.results.geometry.location.lng;
})),
mergeMap(({lat, lon}) => this.ws.getWeatherByCoordinates(lat, lon))
).subscribe((data) => {
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.