Open bradvogel opened 6 years ago
+1
+1
+1
Figured out the answer to my own question. You have to wait for the 'close' event on the filesystem stream:
const csvWriter = require('csv-write-stream');
const fs = require('fs');
const writer = csvWriter();
const stream = fs.createWriteStream('domains.csv');
writer.pipe(stream);
for (....) {
writer.write({...});
}
// Close the writer.
writer.end();
// Wait for the filestream to close.
await new Promise((resolve) => {
stream.on('close', resolve);
});
I found there's a shorter version as end
takes a callback.
await new Promise((resolve) => {
writer.end(resolve);
});
How do you know that it is doing anything?
I would log the stream object into the console, but did not see any change in its state by adding this code.
I used:
await new Promise(resolve => csv.end(resolve));
which doesn't seem to do anything.
Possibly a naive question - how do you flush the writer so it writes what it has to disk? This is important as I want to write my CSV to disk periodically while processing, in case my program crashes.