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

202
Visualizações
Using nested async await function calls

I have a very simple code I'm trying to debug. I have an async function:

async function updateResult(event){

  let result = db.fetchResult(event.ProcessId);
  return result;

}

And I'm calling this from another simple async function:

exports.processEvent = async (event) => {
  try {

    let promises = [];


    const controls = await db.fetchControls(event.EventCode);
    
    if (controls) {     
      
       promises = controls.map((control) => {
         console.log(control);
         control.map(x => this.updateResult(event));
        
       });
           
     }

    return Promise.allSettled(promises);
  }
  catch (err) {
    console.error(err);
  }
};

The problem is that in exports.processEvent, the content of db.fetchControls(event.EventCode) is executed to completion (a few logics and a db getItem call).

But the call to db.fetchResult(event.ProcessId) via this.updateResult(x.Id, x.Version, event) does NOT complete executing the fetchResult tasks (a few other logics a db GET call).

I feel like fetchResult returns prematurely. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

.map() is NOT promise-aware. It does not wait for any of the promises in your callback to complete. So, in this piece of code:

control.map(x => this.updateResult(x.Id, x.Version, event)); 

It just runs through the entire array ignoring all the promises that are returned and thus NOTHING waits for those promises.

You can fix that by returning those promises to the higher level .map() which you are then awaiting with Promise.all():

  promises = controls.map((control) => {
     console.log(control);
     return Promise.all(control.map(x => this.updateResult(x.Id, x.Version, event)));
  });
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