StackOverflow - javascript, node.js, eslint, node-csv-parse, airbnb-js-styleguide
How to avoid for await...of with csv-parse
How about using events end returning promise? const { parse } = require('csv-parse'); const fs = require('fs'); const csvFile = 'myCsvFile.csv'; async function parseCsv(csvFile) { return new Promise((resolve) => { const records = []; const stream = fs.createReadStream(csvFile); const parser = stream.pipe(parse({ delimiter: ',', columns: true })); parser.on('readable', () => { while (record = parser.read()) { records.push(record); } }); let ended = false; const end = (error) => { if (error) { console.error(error.message); } if (!ended) { ended = true; resolve(records); } }; parser.on('error', end); parser.on('end', end); }); } also if You have node 15+ then try stream/promises example: const { parse } = require('csv-parse'); const fs = require('fs'); const { finished } = require('stream/promises'); const csvFile = 'myCsvFile.csv'; async function parseCsv(csvFile) { const records = []; const stream = fs.createReadStream(csvFile); const parser = stream.pipe(parse({ delimiter: ',', columns: true })); parser.on('readable', () => { let record; while ((record = parser.read()) !== null) { records.push(record); } }); await finished(parser); return records; }
Was this helpful?
Related Articles
Have a different question?
Can't find the answer you're looking for? Submit your own question to our community.
🛎️ Get Weekly OTA Fixes
New answers, vendor issues, and updates — straight to your inbox.