Skip to content

Instantly share code, notes, and snippets.

@netsensei
Last active June 7, 2021 17:08
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save netsensei/34831f45da625752faa9 to your computer and use it in GitHub Desktop.
Save netsensei/34831f45da625752faa9 to your computer and use it in GitHub Desktop.
Using the Promise library + Fast-CSV to read/write CSV files
var promiseCSV = require('promiseCSV.js');
var path = "in.csv";
var options = { 'headers': true };
promiseCSV(path, options).then(function (records) {
// do other stuff
});
var csv = require('fast-csv');
var Promise = require('bluebird');
var promiseCSV = Promise.method(function(path, options) {
return new Promise(function(resolve, reject) {
var records = [];
csv
.fromPath(path, options)
.on('data', function(record) {
records.push(record);
})
.on('end', function() {
resolve(records);
});
};
});
module.exports = promiseCSV;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment