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

查看:78
本文介绍了使用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指出的那样,存在

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天全站免登陆