Skip to content

Instantly share code, notes, and snippets.

@ghiden
Created June 15, 2015 09:07
Show Gist options
  • Save ghiden/43d8919eaabab19a1bc5 to your computer and use it in GitHub Desktop.
Save ghiden/43d8919eaabab19a1bc5 to your computer and use it in GitHub Desktop.
To promisify csv-parse using Bluebird
"use strict";
var fs= require('fs');
var Promise = require('bluebird');
var parse= Promise.promisify(require('csv-parse'));
var file = fs.readFileSync('test.csv', 'utf8');
var headerKeys;
var options ={
trim: true,
columns: function(header) {
headerKeys = header;
console.log('header: ', header);
}
};
var users = [];
parse(file, options).then(function(rows) {
rows.forEach(function(r) {
var user = {};
headerKeys.forEach(function(k, i) {
user[k] = r[i];
});
users.push(user);
});
console.log(users);
}).catch(function(err) {
console.error(err);
});
@subatomicglue
Copy link

streaming example would be great. imagine a 2GB csv file? this would kill the system, reading whole file into memory, twice, once for file, again for each row.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment