admin管理员组文章数量:1332389
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
2 Answers
Reset to default 4You 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());
本文标签: javascriptwaiting observable subscribe inside foreach to endStack Overflow
版权声明:本文标题:javascript - waiting observable subscribe inside foreach to end - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742291051a2447804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论