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

196
Visualizações
Await inside createreadStream function not pushing the fetched data into an arrray, Instead goes to next iteration and again fetch

Here I am trying to call every row in a csv file and inside that ".on" we can access every row so I am fetching some data there and then pushing that fetched data into the an array but I see that it call the fetch and then directly goes to next row instead of pushing the data into the array. Any idea on why it is not pushing the data into the array and then go to the next iteration?

var dataArray = [];

fs.createReadStream('./data.csv')
  .pipe(csv())
  .on('data', async function (row) {
    let data = await fetch(row.data);
    console.log(data);
    row.flag = data;
    dataArray.push(row);
  })
  .on('end', async function () {
    console.log('data', dataArray);
    var result = json2csv(dataArray);
    fs.writeFileSync('./file.csv', result);
  });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is how I did it, First pushed all the row data form on("data" )inside createreadstream into an array "arr". Then as @ChrisG suggested, Created a array called promise and pushed all the promises into the array. And at the end called Promiss.all(promises[]).then I got what I wanted. @Bravo

let arr = [];
let promises = []
  fs.createReadStream(path)
  .pipe(csv())
  .on('data', async function (row) {
    arr.push(row)
   
 })
  .on('end',  function () {
    console.log("data",arr.length)
    arr.forEach(async e => {
      let msg
      dataArray.push(e)
     
      promises.push(fetch(msg))
     
      }
    })
    Promise.all(promises).then((data) => {
            console.log("data",dataArray)   
        data.forEach((e, index) => {
          let data = e.data
          dataArray[index].data = data
        })
         var result = json2csv(dataArray);
         fs.writeFileSync(path, result)
   
    })
    })
   


}
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