本文介绍了2角。所有的承诺()与RxJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在角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.

在1角,我就喜欢写东西 $ q.all([$ http.get(...),$ http.get(...),...])。然后(doSomethingWithResponses);

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

但角2返回RxJS观测量和一堆阅读后,我仍然无法弄清楚如何让几个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马丁内斯指出,还有的。 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];
});

这篇关于2角。所有的承诺()与RxJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:35