Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 正在等待subscribe for循环结束,然后继续_Angular_Typescript_Rxjs_Observable - Fatal编程技术网

Angular 正在等待subscribe for循环结束,然后继续

Angular 正在等待subscribe for循环结束,然后继续,angular,typescript,rxjs,observable,Angular,Typescript,Rxjs,Observable,如何检查在将所有结果推送到nodesToExpand时是否完成了以下for循环 getFilterResult是nodeService服务中的http请求调用 for(var step in paths) { this.nodeService.getFilterResult(paths[step]).subscribe( (node) => { nodesToExpand.push(node); } ); }

如何检查在将所有结果推送到nodesToExpand时是否完成了以下for循环

getFilterResult是nodeService服务中的http请求调用

  for(var step in paths)
  {
    this.nodeService.getFilterResult(paths[step]).subscribe(
      (node) =>
      {
        nodesToExpand.push(node);
      }
    );
  }

在我看来,你现在所做的事情是非常错误的。尽量不要将RxJS与for循环混合使用。通常,RxJS具有满足您需求的功能,请看一看

如果要等待多个流直到它们发出事件,可以使用combineLatest()。它不仅告诉您所有http请求何时响应,还以元组结构将数据交给您

const httpCalls:Array<Observable<any>> = paths.map(step => this.nodeService.getFilterResult(step));
combineLatest(HttpCalls)
.subscribe(results => console.log(results));
// --> [Call1Result, Call2Result, ...]
consthttpcalls:Array=path.map(步骤=>this.nodeService.getFilterResult(步骤));
组合相关测试(HttpCalls)
.subscribe(results=>console.log(results));
//-->[Call1Result,Call2Result,…]

我觉得你现在所做的事情非常错误。尽量不要将RxJS与for循环混合使用。通常,RxJS具有满足您需求的功能,请看一看

如果要等待多个流直到它们发出事件,可以使用combineLatest()。它不仅告诉您所有http请求何时响应,还以元组结构将数据交给您

const httpCalls:Array<Observable<any>> = paths.map(step => this.nodeService.getFilterResult(step));
combineLatest(HttpCalls)
.subscribe(results => console.log(results));
// --> [Call1Result, Call2Result, ...]
consthttpcalls:Array=path.map(步骤=>this.nodeService.getFilterResult(步骤));
组合相关测试(HttpCalls)
.subscribe(results=>console.log(results));
//-->[Call1Result,Call2Result,…]

这是否确保结果按其被调用的顺序返回?是的,HttpCalls的顺序很重要,而不是在解析时。您可以在这里检查:这是否确保结果按其被调用的顺序返回?是的,HttpCalls的顺序很重要,而不是在它们解析时。您可以在这里查看: