Last active
June 7, 2021 17:08
-
-
Save netsensei/34831f45da625752faa9 to your computer and use it in GitHub Desktop.
Using the Promise library + Fast-CSV to read/write CSV files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var promiseCSV = require('promiseCSV.js'); | |
var path = "in.csv"; | |
var options = { 'headers': true }; | |
promiseCSV(path, options).then(function (records) { | |
// do other stuff | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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