Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda