Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!