执行http请求时带有多个forkjoin的rxjs [英] rxjs with multiple forkjoin when doing http requests

查看:20
本文介绍了执行http请求时带有多个forkjoin的rxjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一些 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 方法中迭代报告数组,读取一些值,并为每个报告再次使用这些值创建一个新的 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]
    });
}

这应该可以,它更像是一种old-style-logic-hammer"方法 - 如果您正在寻找它:可能有一种更优雅的方法来解决这个问题:其他操作符,但如果不了解完整数据和所有逻辑,就很难说.

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.

这篇关于执行http请求时带有多个forkjoin的rxjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆