Angular2 Observable - how to wait for all function

2019-03-22 15:18发布

问题:

I am trying to improve my knowledge of Angular2 by migrating an application currently written in Angular1

One feature in particular has me stumped. I am trying to replicate a feature where a calling function waits to proceed until the function it is calling has completed a loop of promises. In angular one the function I am calling looks basically like this:

this.processStuff = function( inputarray, parentnodeid ) {
    var promises = [];
    var _self = this;
    angular.forEach( inputarray , function( nodeid ) {

        switch ( parentnodeid )
        {
            case ‘AAA’ : var component = _self.doMoreStuff( nodeid, parentnodeid ); break;
            case ‘BBB’ : var component = _self.doMoreStuff( nodeid, parentnodeid ); break;
            case ‘CCC’ : var component = _self.doMoreStuff( nodeid, parentnodeid ); break;
            default    : var component = null;
        }
        promises.push( component );
    });
    return $q.all(promises);
}; 

It contains a forEach loop that calls another function (doMoreStuff) that also returns a promise and it stores all those returned promises in an array.

With Angular1, when I call processStuff in another function, I could count on the system waiting until processStuff completed before entering into the code in the then block. IE:

service.processStuff( arraying, parentidarg )
       .then(function( data ) {
              ... 

The caller of processStuff waits for all doMoreStuff invocations to complete until the caller of processStuff enters into its then block.

I am unsure of how to achieve this with Angular2 and Observables. I can see from these posts that to mimic promises, Observables basically just use subscribe instead of then:

Angular 1.x $q to Angular 2.0 beta

But how do I wait for all invocations in the forEach loop to complete before my system can proceed?

回答1:

I have been doing this with forkJoin

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';

Observable.forkJoin(
  this.http.get('./friends.json').map((res: Response) => res.json()),
  this.http.get('./customer.json').map((res: Response) => res.json())
)
.subscribe(res => this.combined = {friends: res[0].friends, customer: res[1]});

Some more info here: http://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http