我从一个httpService获得一个RxJS Observable,这是来自Angular的实际http.现在,当我从中得到一个积极的结果时,我想处理从this.retrieve()得到的下一个http请求.这或多或少是连续请求.有没有更好的方法呢?
return this.httpService.query(data)
.map(data => {
if(data.status > 1)
this.retrieve().subscribe();
return data;
});
解决方法:
使用Observable.flatMap运算符可以实现链接HTTP请求.假设我们想要发出三个请求,其中每个请求取决于前一个请求的结果:
this.service.firstMethod()
.flatMap(firstMethodResult => this.service.secondMethod(firstMethodResult))
.flatMap(secondMethodResult => this.service.thirdMethod(secondMethodResult))
.subscribe(thirdMethodResult => {
console.log(thirdMethodResult);
});
这样,您可以链接所需的相互依赖的请求.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。