Created
June 15, 2015 09:07
-
-
Save ghiden/43d8919eaabab19a1bc5 to your computer and use it in GitHub Desktop.
To promisify csv-parse using Bluebird
This file contains 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
"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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.