Skip to content

Instantly share code, notes, and snippets.

@zachsa
Created August 7, 2019 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachsa/aeb4bba66c38ccf8a8e710039ac7e69e to your computer and use it in GitHub Desktop.
Save zachsa/aeb4bba66c38ccf8a8e710039ac7e69e to your computer and use it in GitHub Desktop.
An example of iteratively reading a CSV file using the popular 'csv' library
// https://dev.to/maheshkay/read-csv-file-using-node-js-29oc
const parse = require('csv-parse')
const fs = require('fs')
const { createReadStream } = fs
module.exports = async FILEPATH => {
const contents = []
await new Promise((resolve, reject) => {
createReadStream(FILEPATH)
.pipe(parse({ delimiter: ',' }))
.on('data', data => contents.push(data))
.on('error', err => reject('ReadStream error: ' + err))
.on('end', resolve)
})
return contents
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment