Skip to content

Instantly share code, notes, and snippets.

@Muzietto
Last active March 8, 2017 04:20
Show Gist options
  • Save Muzietto/0c45d18335f8e0ea10020839592fb919 to your computer and use it in GitHub Desktop.
Save Muzietto/0c45d18335f8e0ea10020839592fb919 to your computer and use it in GitHub Desktop.
Promise to write data into a CSV file using npm module 'csv-write-stream'
putPortfolioData = function(result, dataSinkName) {
return new Promise(function (resolve, reject) {
var filename = __dirname + dataSinkName,
writer = csvStream(),
ws = fs.createWriteStream(filename)
;
writer.on('error', function(error) {
reject(error);
});
writer.on('end', function() {
resolve(result);
})
writer.pipe(ws);
result.forEach(function(obj) {
writer.write(obj);
});
writer.end();
});
};
@Muzietto
Copy link
Author

Muzietto commented Mar 7, 2017

This code works flawlessly when running tests with Mocha. When it is invoked by the real nodejs app, the CSV file is created, but it is empty and no warning/error whatsoever is thrown. Pls see this question on SO

I am working on Windows 7. Node 6.9.4, Npm 3.5.3, csv-write-stream 2.0.0.

@Muzietto
Copy link
Author

Muzietto commented Mar 8, 2017

Solved my issue by resolving the promise inside ws.on('finish', callback). See here.

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