javascript - waiting observable subscribe inside foreach to end - Stack Overflow

Im iterating over an array of objects and for each iteration i run a observable.subscribe, how can i en

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 pleted, so i can call another function?

this is the 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();

  }

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

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 pleted, so i can call another function?

this is the 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();

  }

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

Share edited Jun 6, 2018 at 13:26 Daniel Bezerra De Menezes asked Jun 6, 2018 at 13:09 Daniel Bezerra De MenezesDaniel Bezerra De Menezes 1,7366 gold badges26 silver badges47 bronze badges 3
  • You want to call once when all the subscribe is done? Or call every time when each of the subscribe is done? – Will Huang Commented Jun 6, 2018 at 13:23
  • when all subscribe are done – Daniel Bezerra De Menezes Commented Jun 6, 2018 at 13:25
  • Possible duplicate of Angular 5 & Rxjs: Wait for all subscription – ADreNaLiNe-DJ Commented Jun 6, 2018 at 13:28
Add a ment  | 

2 Answers 2

Reset to default 4

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();
        }
    );
}

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

import { forkJoin } from 'rxjs';

In RxJS 5 it could look like this. In RxJS 6 just replace Observable.forkJoin with 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());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742291051a4416196.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信