等待效果循环中的方法 angular

Waiting for a method in a loop in effects angular

如何等待循环完成并获取数据?想过forkJoin方法,但是没有成功...

    getEntities$ = createEffect(() =>
    this.actions$.pipe(
      ofType(GetEntities),
      mergeMap((s: any) =>
        // first request
        this.service.getEntities(s.id).pipe(
          map((data: Entitites[]) => {
            const entities = cloneDeep(data);
            for (const entity of entities) {
              // second request
              this.service.getAdditionalInfo(s.id, entity.id).subscribe((info: Info) => {
                entity.info = info;
              })
            }
            // getting the old data without additional info
            return entities;
          }),
          mergeMap((entities: Entities[]) => [GetEntitiesSuccess({ entities})]),
          ....

您可以使用 concatforkJoin 实现,如下所示:

getEntities$ = createEffect(() =>
    this.actions$.pipe(
        ofType(GetEntities),
        mergeMap((s: any) => this.service.getEntities(s.id)),
        mergeMap((data: Entitites[]) => {
            const entities = cloneDeep(data);
            return concat(
                ...entities.map((entity) => 
                    this.service.getAdditionalInfo(s.id, entity.id).pipe(map(info: Info) => {
                        entity.info = info;
                        return entity;
                    })
                )
            );
        }),
        mergeMap((entities: Entities[]) => [GetEntitiesSuccess({ entities })])
    )
);