Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
Typescript: Convert For loop to Promise and wait to resolve all

ISSUE: save.emit() runs before all the iterations are completed in the below "for" loop which shows incorrect values of the addUpdate call inside loop.

I am trying to convert a for loop to promise and then wait for each of those promise to resolve so that I can emit changes and close a popup.

Below, is a test code in which I want to print "console.log("Print Before")" first for each iteration and then at the end print "console.log("Print After")" once all iterations are done.

Any help on the syntax for this is really appreciated.

  convertForLoopToPromiseAndWait(someParameterOfTypeObject) {
    for (var test of someParameterOfTypeObject) {
      var testVariable = test.setValue;
      if (testVariable) {         
          dataService.addUpdateEndpointCall();
          console.log("Print Before");    
      }
    }
    console.log("Print After");
    save.emit();
  }

      async addUpdateEndpointCall() {
        const promise1 =  this.dataService.addCall().take(1).toPromise();
        const promise2 =  this.dataService.deleteCall().take(1).toPromise();
    
        await Promise.all([promise1, promise2])
          .then(_ => this.save.emit());
      }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Convert convertForLoopToPromiseAndWait to async method, then you can use await after for keyword and before dataService.addUpdateEndpointCall();

async convertForLoopToPromiseAndWait(someParameterOfTypeObject) {
  for await (var test of someParameterOfTypeObject) {
    var testVariable = test.setValue;
    if (testVariable) {         
      await dataService.addUpdateEndpointCall();
      console.log("Print Before");    
    }
  }
  console.log("Print After");
  await save.emit();
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think that you have made a mistake here:

const promise1 = await this.dataService.addCall().take(1).toPromise();
const promise2 = await this.dataService.deleteCall().take(1).toPromise();

You await promises. The results put in the variables promise1 and promise2 will then not be promises.

Don't you mean the following?

      async addUpdateEndpointCall() {
        const promise1 = this.dataService.addCall().take(1).toPromise();
        const promise2 = this.dataService.deleteCall().take(1).toPromise();
    
        await Promise.all([promise1, promise2])
          .then(_ => this.save.emit());
      }
about 4 years ago · Juan Pablo Isaza Relatório

0

Another way is to make a list of promises and wait for them to resolve:

const promises = [];
        for await (your iterating condition) {
          promises.push(dataService.addUpdateEndpointCall());
        }

then use

await Promise.all(promises).then( this.save.emit());
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda