0

I've this piece of code

var fs = require('fs');
var csv = require('fast-csv');
const dishTypes = [];

        fs.createReadStream('dishtypes_id.csv')
            .pipe(csv())

            .on('data', function (data) {
                dishTypes.push(data)
            })
            .on('end', function (data) {
                console.log("END", data)

            })



console.log("DISHTYPES",dishTypes)

Output is this

DISHTYPES []
END 3839

I can imagine what's going on here. But I can't tackle it. dishTypes array should populate with 3839 objects. But right now it's empty. Because this fast-csv thing works in async way. How do I tackle that? I mean console.log should run after the end function.

Zoe
  • 27,060
  • 21
  • 118
  • 148
margherita pizza
  • 6,623
  • 23
  • 84
  • 152

0 Answers0