【问题标题】:waiting observable subscribe inside foreach to end等待 foreach 内的可观察订阅结束
【发布时间】:2018-11-16 04:35:51
【问题描述】:

我在一个对象数组上进行迭代,每次迭代我都运行一个 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()。有什么建议吗?

【问题讨论】:

标签: javascript typescript rxjs


【解决方案1】:

您可以尝试将forkJoin 与onCompleted 回调一起使用。见下文:

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';

【讨论】:

  • 真的不起作用,第二个 foreach 需要在第一个里面,每个 cobertura 运行第二个代码块并添加一个 MovimentosProjetados 数组 =/
  • @DanielBezerraDeMenezes 你使用哪个 RxJS 版本?
  • 5.5 有关系吗?
  • @DanielBezerraDeMenezes 我已经更新了我的答案。你要不要试一试?
  • 很好!!!!!!!!!完全按照我的需要工作,我真的需要提高我的 rxjs 技能,如果你有任何关于在哪里搜索学习材料的建议,我会很感激,谢谢你的时间!祝你有美好的一天。
【解决方案2】:

在 RxJS 5 中,它可能看起来像这样。在 RxJS 6 中,只需将 Observable.forkJoin 替换为 forkJoin。

const observables = [];

this.cliente.coberturas.forEach(cobertura => {
  // just create the Observable here but don't subscribe yet
  observables.push(this._dataService.ObterSimulacao(...));
});

Observable.forkJoin(observables)
  .subscribe(results => this.calcularSimulacao());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    相关资源
    最近更新 更多