javascript - 在 foreach 中等待可观察订阅结束

标签 javascript typescript rxjs

我迭代对象数组,每次迭代我都运行一个 observable.subscribe,我如何确保所有订阅都已完成,以便我可以调用另一个函数?

这是函数

calculaSimulacoesPorInscricao(){
    let lista = ["2019-01-01","2020-02-02","2021-01-01","2022-01-01","2023-01-01"];
    this.cliente.coberturas.forEach(cobertura => {
      cobertura.MovimentosProjetados = [];
      this._dataService.ObterSimulacao(cobertura.codigoInscricao,lista)
      .subscribe((data:any[])=>{
        data[0].simulacaoRentabilidadeEntities.forEach(simulacao =>{ 
          let movimento = {
            dataMovimento: '',
            valor: 1,
            imposto: 1,
            percentualCarregamento: 1,
            fundoCotacao: []
          };         
        movimento.dataMovimento = simulacao.anoRentabilidade;
        movimento.imposto = cobertura.totalFundos * simulacao.demonstrativo.demonstrativo[0].aliquota;
        movimento.percentualCarregamento = simulacao.valorPercentualCarregamento * (cobertura.totalFundos + (cobertura.totalFundos * simulacao.percentualRentabilidade));
        movimento.valor = cobertura.totalFundos + (cobertura.totalFundos * simulacao.percentualRentabilidade);
        cobertura.MovimentosProjetados.push(movimento);
        });
      })
    });
    this.calcularSimulacao();

  }

在 coberturas.foreach 中的所有订阅完成后,我需要调用 calcularSimulacao()。有什么建议吗?

最佳答案

您可以尝试使用 forkJoinonCompleted 回调。见下文:

calculaSimulacoesPorInscricao() {
    let lista = [
        '2019-01-01',
        '2020-02-02',
        '2021-01-01',
        '2022-01-01',
        '2023-01-01'
    ];
    let all_obs = [];
    this.cliente.coberturas.forEach(cobertura => {
        cobertura.MovimentosProjetados = [];
        all_obs.push(
            this._dataService.ObterSimulacao(cobertura.codigoInscricao, lista).pipe(
                map(
                    (data: any[]) => {
                        data[0].simulacaoRentabilidadeEntities.forEach(simulacao => {
                            let movimento = {
                                dataMovimento: '',
                                valor: 1,
                                imposto: 1,
                                percentualCarregamento: 1,
                                fundoCotacao: []
                            };
                            movimento.dataMovimento = simulacao.anoRentabilidade;
                            movimento.imposto =
                                cobertura.totalFundos *
                                simulacao.demonstrativo.demonstrativo[0].aliquota;
                            movimento.percentualCarregamento =
                                simulacao.valorPercentualCarregamento *
                                (cobertura.totalFundos +
                                    cobertura.totalFundos * simulacao.percentualRentabilidade);
                            movimento.valor =
                                cobertura.totalFundos +
                                cobertura.totalFundos * simulacao.percentualRentabilidade;
                            cobertura.MovimentosProjetados.push(movimento);
                        });
                    })
            )
        );
    });

    forkJoin(all_obs).subscribe(
        undefined,
        undefined,
        () => {
            this.calcularSimulacao();
        }
    );
}

如果您使用的是 RxJS 6,请记住导入 forkJoin

import { forkJoin } from 'rxjs';

关于javascript - 在 foreach 中等待可观察订阅结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50721283/

相关文章:

javascript - 如何使div网格相互忽略

javascript - 如何配置 karma 以便 typescript 源文件是可调试的

javascript - 用于 res.send 的 Express.js 中间件,在每个 res.send() 上调用函数

Angular 阻塞(同步)http 调用

javascript - 在html中显示javascript变量

javascript - 突出显示由异步请求数据动态填充的表中的一行

php - 将 XMLHttpRequest.responseText 保存在变量中

javascript - 类型 string[] 不可分配给类型 'IntrinsicAttributes & string[]'

javascript - RxJS:延迟传入的 observables

rxjs - RxJs API 中的 bindCallback 和 bindNodeCallback 有什么区别?