For not waiting to complete request, it continues the count

2

I'm developing an ionic application and I'm having a problem when looping. In this loop there is a call from a method of a provider, which makes a request put . My problem is that for does not expect the server to return it to continue execution, how can I synchronize for with the request?

My code below:

getAtualizados(){

let atualizados: Cadastrolist[]=[];

this.contribuinteProvider.getAtualizados()
.then((result)=>{
  atualizados = result;
  for (var i = 0; i < atualizados.length; i++){
    this.servidorProvider.update(atualizados[i].dados.cadastro)
    .then((result)=>{
      this.contribuinteProvider.remove(atualizados[i].key);
    });
  }

});}
    
asked by anonymous 08.06.2018 / 16:37

1 answer

2

I was able to solve it, for this I used Async / await.

async update(cadastro, id){
await this.servidorProvider.update(cadastro)
.then((result)=>{
  this.contribuinteProvider.remove(id);
});
}
async getAtualizados(){
let atualizados: Cadastrolist[]=[];

await this.contribuinteProvider.getAtualizados()
.then((result)=>{
  atualizados = result;
});

for (var i = 0; i < atualizados.length; i++){
 await this.update(atualizados[i].dados.cadastro, atualizados[i].key);
}

}

    
15.06.2018 / 16:22