0

So it's really quite simple but I can't seem to get it to work. I'm trying to pull in a csv file , turn the rows to objects, then push to objects to an array then do stuff with the array. Everytime I try to run it, it wont await the filestream or let me read the modified reports array.

    const csv = require('csv-parser');
    const fs = require('fs');
    const reports = [];
    
    
    async function readTable(){
        fs.createReadStream('pull.csv')
        .pipe(csv())
        .on('data', (row) => {
            //console.log(row);
            reports.push(row)
        })
        .on('end', () => {
            console.log('got all the csv data')
            return 
        });
    }
    
    async function getData(){
        await readTable()
        console.log(reports)
    }
    
    
  readTable(); 
Wells
  • 55
  • 7
  • When do you want `readTable` to complete? And what value would you like its promise to resolve with? – Wyck May 11 '21 at 20:05
  • As soon as it's done with reading all of the rows from the CSV, I just want the promise to resolve with reports array that has all of the table items as objects @Wyck – Wells May 11 '21 at 20:06
  • 1
    See https://stackoverflow.com/questions/33599688/how-to-use-es8-async-await-with-streams – Wyck May 11 '21 at 20:06
  • Why are you expecting anything to wait for the stream? – Bergi May 11 '21 at 23:13

0 Answers0