我有一个应用程序可以创建3个HTTP请求,并且每个请求都依赖于来自其他人的信息。目前,我正在提出下一个成功的请求,但效率不高。我做了一些研究,发现我假设使用flatmap
是,但我已经试过几个方法不使HTTP请求的API如何正确平面映射三个HTTP请求
这是我做过尝试,但它没有返回
return this.info.getLastRecord(this.station)
.map((res: any) => res.json()
.flatMap((info: any) => {
return this.info.getQueue(this.lastID)
.map((res: any) => {
console.log(res.json());
});
}));
这不工作或者
return this.info.getLastRecord(this.station)
.map((res: any) => res.json()
.flatMap((res: any) => {
return this.info.getQueue(this.lastID)
.map((res: any) => {
console.log(res);
});
}));
这是如何我目前正在调用
this.getInfoService.getLastRecord(this.station)
.subscribe(
(info) => {
console.log("res", info);
},
(error: Response) => console.log(error),
() => this.getQueue()
);
getQueue() {
this.info.getQueue(this.lastID)
.subscribe(
(info) => {
console.log("getQueue", info);
},
(error: Response) => console.log(error),
() => this.gHTDG())
}
getHookTypeDieGroup() {
this.info.getHookTypeDieGroup(this.profileCode)
.subscribe(
(info) => {
console.log("GHTDG", info);
},
(error: Response) => console.log(error)
);
}
个
info.service.ts
getLastRecord(station: string): any {
return this.http.get('http://API.app/getLastRecord/' + station)
.map(
(response: Response) => {
return response.json().info;
},
).share();
}
getQueue(lastID: number): any {
return this.http.get('http://API.app/getQueue/' + lastID)
.map(
(response: Response) => {
return response.json().info;
}
)
.share();
}
gHTDG(pCode: string): any {
return this.http.get('http://API.app/getHTDG/' + pCode)
.map(
(response: Response) => {
return response.json().info;
}
).share();
}
。我认为它按照你想要的方式工作。 –