Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript foreach内部等待可观察到的订阅结束_Javascript_Typescript_Rxjs - Fatal编程技术网

Javascript foreach内部等待可观察到的订阅结束

Javascript foreach内部等待可观察到的订阅结束,javascript,typescript,rxjs,Javascript,Typescript,Rxjs,我在一个对象数组上迭代,每次迭代我都运行一个observable.subscribe,我如何确保所有的subscribe都完成了,这样我就可以调用另一个函数了 这就是函数 calculaSimulacoesPorInscricao(){ let lista = ["2019-01-01","2020-02-02","2021-01-01","2022-01-01","2023-01-01"]; this.cliente.coberturas.forEach(cobertura =

我在一个对象数组上迭代,每次迭代我都运行一个observable.subscribe,我如何确保所有的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()。有什么提示吗?

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

在RXJS5中,它可能看起来像这样。在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());

您可以尝试将
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';

您可以尝试将
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需要在第一个foreach内,每个cobertura运行第二个代码块并添加一个MovimentosProjetados=/@DanielBezerraDeMenezes数组,您使用哪个RxJS版本?5.5是否相关?@DanielBezerraDeMenezes我已经更新了我的答案。你能试试吗?很好!!!!!!!!!非常适合我,我真的需要提高我的rxjs技能,如果你对在哪里搜索学习材料有任何建议,我会通知你,谢谢你的时间!祝你有一个愉快的一天。这真的不起作用,第二个foreach需要在第一个foreach中,每个cobertura运行第二个代码块并添加一个movientosprojetados数组=/@DanielBezerraDeMenezes你使用哪个RxJS版本?5.5是否相关?@DanielBezerraDeMenezes我已经更新了我的答案。你能试试吗?很好!!!!!!!!!非常适合我,我真的需要提高我的rxjs技能,如果你对在哪里搜索学习材料有任何建议,我会通知你,谢谢你的时间!祝您有个美好的一天。