使用 RxJS 承诺 .all() [英] Promise .all() with RxJS

查看:21
本文介绍了使用 RxJS 承诺 .all()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Angular 2 编写一个应用程序,我想执行几个 http 请求并对响应运行一个函数.

I'm writing an app in Angular 2 and I want to execute several http requests and run a function on the responses.

在 Angular 1 中,我会写类似 $q.all([$http.get(...), $http.get(...), ...]).then(doSomethingWithResponses);

In Angular 1, I would write something like $q.all([$http.get(...), $http.get(...), ...]).then(doSomethingWithResponses);

但是 Angular 2 返回 RxJS Observables,经过大量阅读后,我仍然无法弄清楚如何获取多个 http 请求的响应.这怎么可能?

But Angular 2 returns RxJS Observables and after a bunch of reading I still can't figure out how to get the responses of several http requests. How can this can be done?

推荐答案

正如@Eric Martinez 所指出的,有 forkJoin.forkJoin 并行运行所有可观察序列并收集它们的最后元素.

As @Eric Martinez pointed out, there is forkJoin. forkJoin runs all observable sequences in parallel and collect their last elements.

Rx.Observable.forkJoin([a,b]).subscribe(t=> {
        var firstResult = t[0];
        var secondResult = t[1];
});

这篇关于使用 RxJS 承诺 .all()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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