在 Angular 服务中,假设 map 函数在 Observable 内部使用时保证同步是否正确?

In an Angular service, is it correct to assume the map function is guaranteed to be synchronous if used inside of an Observable?

在一个 Angular 应用程序中,我有一个订阅服务中 Observable 的组件。

this.listService.listApi(params)
          .subscribe(
            response => this.listServiceResponse(response),
            error => this.serviceError = <any>error);

在服务中,observable 发出了一个 http 请求。我需要遍历响应并根据一些计算构建一个结果数组,这将是最终返回给组件的内容。

为了清楚起见,我在这里简化了生成的数组,因为我的要复杂得多,但这里的重点是我使用 map 函数实现了这一点。以下是服务中方法的相关部分:

listApi(params:any) Observable<any[]> {
    ...
    return this.http.get(params.url, ampOptions)
                        .map(data => this.listExtract(response, params))
                        .catch(this.handleError);
}
listExtract(response:any, params:any) {

  let successArray:any = [];
  let failedArray:any = [];

  let filteredArray:any = [];

  let itemFound:boolean = false;

  response.map((item, index) => {
    if(item.rank > params.rank) {
      filteredArray.push(item);
      if(item.id === params.id) {
        itemFound = true;
      }
    }
  });

  // are the following lines guaranteed to wait for the map function to finish?

  if(itemFound === true) {

    // do some additional calculations and construct the successArray
    // then push the filteredArray onto my successArray;

    successArray.push(filteredArray);

    return successArray;

  } else {

    // do some additional calculations and construct the failedArray
    // then push the filteredArray onto my failedArray;

    failedArray.push({flag:'failed',id:params.id});
    failedArray.push(filteredArray);

    return failedArray;
  }

}

这个 看起来 工作正常,但是可以安全地假设 map 函数是同步的并且后面的行保证只执行 地图功能完成后?

如果 response 保证是一个数组——为了不破坏你的代码——那么是的,its map() function is synchronous