带有多个forkjoin的rxjs在执行HTTP请求时 [英] rxjs with multiple forkjoin when doing http requests
问题描述
我正在执行一些http请求,并使用rxjs成功通知结果:
I am doing some http requests and use rxjs for successfull notification of the result:
getReportings(departmentId: number): Observable<any> {
return Observable.forkJoin(
this.http.get('/api/members/' + departmentId).map(res => res.json()),
this.http.get('/api/reports/' + departmentId).map(res => res.json())
);
}
当两个HTTP请求都完成后,我希望在getReportings方法内迭代reports数组,读取一些值,并为每个报告再次使用这些值进行新的http请求.
When both http requests are done I want inside the getReportings method to iterate the reports array , read some values and for each report make again a new http request with those values.
总共我有2个(成员/报告)+ appr. 4到8个(其他内容)请求.
All in all I have 2 (member/reports) + appr. 4 to 8 (other stuff) requests.
全部通过时.完成6到8个请求后,我想在成功处理程序中获取前6到8个请求中的所有数据.
When all appr. 6 to 8 requests are done I want to get ALL data from the previous 6 to 8 requests in the successfull handler.
如何使用rxjs做到这一点?
How can I do this with rxjs?
更新
随着olsn用户要求提供更多详细信息,现在我了解他的关注点,我在这里放置了更多数据(伪代码),6至8个请求应如何显示:
As user olsn asked for more details and I understand him now whats his concern I put more data here (pseudo code) how the 6 to 8 requests should look like:
getReportings(departmentId: number): Observable<any> {
return Observable.forkJoin(
this.http.get('/api/members/' + departmentId).map(res => res.json()),
this.http.get('/api/reports/' + departmentId).map(res => res.json())
).switchMap((result: [any[], any[]]) => {
let members: any[] = result[0];
let reports: any[] = result[1];
let allNewStreams: Observable<any>[] = [
Observable.of(members),
Observable.of(reports)
];
for(let report of reports)
{
allNewStreams.push(
this.http.get(report.url + ?key1=report.name1?).map(res => res.json()));
}
return Observable.forkJoin(allNewStreams); // will contain members, reports + 4-6 other results in an array [members[], reports[], ...other stuff]
});
}
推荐答案
您可以使用switchMap
扩展流,如下所示:
You could extend your stream using switchMap
, like this:
getReportings(departmentId: number): Observable<any> {
return Observable.forkJoin(
this.http.get('/api/members/' + departmentId).map(res => res.json()),
this.http.get('/api/reports/' + departmentId).map(res => res.json())
).switchMap((result: [any[], any[]]) => {
let members: any[] = result[0];
let reports: any[] = result[1];
let allNewStreams: Observable<any>[] = [
Observable.of(members),
Observable.of(reports)
];
// do your stuff and push new streams to array...
if (foo) { // for each additional request
let reportId: string | number = reports[0].id; // or however you retrieve the reportId
allNewStreams.push(
this.http.get('some/api/ + bar)
.map(res => res.json())
.map(data => ({reportId, data})); // so your final object will look like this: {reportId: "d38f68989af87d987f8", data: {...}}
);
}
return Observable.forkJoin(allNewStreams); // will contain members, reports + 4-6 other results in an array [members[], reports[], ...other stuff]
});
}
这应该做到,它更像是一种旧式逻辑锤" 方法-如果您正在寻找它:可能有一种更优雅的方法来解决此问题其他运算符,但是如果不知道全部数据和所有逻辑就很难说.
This should do it, it's more like an "old-style-logic-hammer" approach - in case you are looking for it: There might be a more elegant way to solve this by using other operators, but that is hard to say without knowing the full data and all logic.
这篇关于带有多个forkjoin的rxjs在执行HTTP请求时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!