Javascript Angular2 Observable - 如何在继续之前等待循环中的所有函数调用结束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35566931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:54:16  来源:igfitidea点击:

Angular2 Observable - how to wait for all function calls in a loop to end before proceeding?

javascriptangularobservableangular-promise

提问by Benjamin McFerren

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

我正在尝试通过迁移当前用 Angular1 编写的应用程序来提高我对 Angular2 的了解

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.

它包含一个 forEach 循环,该循环调用另一个函数 (doMoreStuff),该函数也返回一个承诺,并将所有这些返回的承诺存储在一个数组中。

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:

使用 Angular1,当我在另一个函数中调用 processStuff 时,我可以指望系统等待 processStuff 完成,然后再进入 then 块中的代码。IE:

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

The caller of processStuffwaits for all doMoreStuffinvocations to complete until the caller of processStuffenters into its then block.

的调用者processStuff等待所有doMoreStuff调用完成,直到调用者processStuff进入其 then 块。

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:

我不确定如何使用 Angular2 和 Observables 来实现这一点。我可以从这些帖子中看到,为了模仿承诺,Observables 基本上只是使用 subscribe 而不是 then:

Angular 1.x $q to Angular 2.0 beta

Angular 1.x $q 到 Angular 2.0 测试版

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

但是我如何forEach在系统可以继续之前等待循环中的所有调用完成?

回答by TGH

I have been doing this with forkJoin

我一直在用 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

这里有更多信息:http: //www.syntaxsuccess.com/viewarticle/angular-2.0-and-http