【发布时间】:2019-06-05 21:53:24
【问题描述】:
是否可以在不更改为 Promises 的情况下等待可观察的 rxjs? 我正在执行 6 个请求 OnInit() 但是,一些请求在另一个请求之前完成,顺序很重要。是否可以在 typecrypt 中等待 observable?所有功能看起来都一样,它们只是向服务器发出不同的请求。 谢谢您的帮助 :) 这是我的代码:
我设法通过链接请求来实现它(在我得到前一个请求的响应后调用下一个请求,但是,该解决方案效率不高而且看起来很丑)
ngOnInit() {
this.userId = this.auth.getUserId();
this.getTrendingRentals();
this.getFollowingRentals();
...
this.getLuxuryRentals();
//console.log(this.totalRentals);
}
private getTrendingRentals() {
const rentalObservable = this.rentalService.getRentals();
//Subscribe to service observer
rentalObservable.subscribe((rentals: Rental[]) => {
this.trendingRentals = rentals;
this.totalRentals.push(this.trendingRentals);
}, (err) => {
}, () => {
});
}
public getLuxuryRentals(): Observable<any>{
return this.http.get('/api/v1/rentals/luxury');
}
【问题讨论】:
标签: angular typescript asynchronous async-await rxjs