等待 observable 在 foreach 内订阅结束 [英] waiting observable subscribe inside foreach to end

查看:26
本文介绍了等待 observable 在 foreach 内订阅结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im iterating over an array of objects and for each iteration i run a observable.subscribe, how can i ensure that the all the subscribes were completed, so i can call another function?

这是函数

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().有什么提示吗?

i need to call calcularSimulacao() after all subscribe inside the coberturas.foreach is done. Any tips?

推荐答案

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

You can try using forkJoin with onCompleted callback. See below:

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.

Just remember to import forkJoin if you're using RxJS 6.

import { forkJoin } from 'rxjs';

这篇关于等待 observable 在 foreach 内订阅结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆