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

131
Visualizações
How to wait for all in Javascript

My code is nearly working well my problem is that only the first CSV data is logged and the lambda function ends.

I guess I need someway to wait for all stream pipes to end.

myCsvList.forEach((myElem) => {
      const data = [];

      // setup params

      const csvFile = s3.getObject(params).createReadStream(); // works fine

      csvFile
        .pipe(csv())
        .on('data', function(entry) {
          data.push(entry);
        })
        .on('end', () => {
          console.log(data); // after the log of the first element on myCsvList, the code finishes. It should log all csvFiles from myCsvList
        });
});

I guess I need a promises or something?

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

0

you can change every item to Promise, then use Promise.all():

const getItem = (myElem) => {
  const data = [];

  // setup params

  const csvFile = s3.getObject(params).createReadStream(); // works fine

  return new Promise((resolve) => {
    csvFile
      .pipe(csv())
      .on('data', function (entry) {
        data.push(entry);
      })
      .on('end', () => {
        resolve(data); // after the log of the first element on myCsvList, the code finishes. It should log all csvFiles from myCsvList
      });
  });
};

const start = () => {
  // all promises have been finished
  return Promise.all(myCsvList.map(myElem => getItem(myElem)));
}

myCsvList.map(myElem => getItem(myElem)) can get a promise list, when all promises have been resolved, it will trigger Promise.all().

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