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

108
Vistas
Synchronisation of createReadStream

I'm very noob at Javascript. All I'm trying to do is read a CSV file from local and playaround with the data, like converting the rows into arrays of array. I came across the fs.createReadStream, which helps to read csv data but I'm unable to utilize the processed csv data later. I read in some thread, and I guess the issue with the synchronisation. My code looks like this-

ar abc = [] 
const csv = require('csv-parser');
const fs = require('fs');

fs.createReadStream('data.csv')
  .pipe(csv())
  .on('data', (row) => {
    var nums =[];

   // console.log(row);
    nums.push(row['time'])
    nums.push(row['return'])
    
    abc.push(nums)
    
  })
  .on('end', function () {
    console.log(abc)
      })
  

  var final_sort= []
  console.log()
  for (let i = 0; i < abc.length; i=i+5){

    final_sort.push(abc[i])


  } 
  console.log(final_sort)

this outputs log outside fs first then outputs the fs.createReadStream. I wanted the fs. createReadStream to be processed first, then I can utilize the data further.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

if you want to handle your sequence then you have to try promises

if your task is read all data and then sort then your code will be like

var abc = []
const csv = require('csv-parser');
const fs = require('fs');
// -----------1 process start
await new Promise((resolve, reject) => {
  fs.createReadStream('data.csv')
    .pipe(csv())
    .on('data', (row) => {
      var nums = [];

      // console.log(row);
      nums.push(row['time'])
      nums.push(row['return'])

      abc.push(nums)

    })
    .on('end', function () {
      console.log(abc)
      //---------- 2 return after all data read
      resolve()
    })
});
//---------- 3 here you get all data
var final_sort = []
console.log()
for (let i = 0; i < abc.length; i = i + 5) {

  final_sort.push(abc[i])


}
console.log(final_sort)

for more refenerce understanding-javascript-promises

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