max-mapper / csv-write-stream

A CSV encoder stream that produces properly escaped CSVs
BSD 2-Clause "Simplified" License
204 stars 37 forks source link

How to flush the writer? #34

Open bradvogel opened 6 years ago

bradvogel commented 6 years ago

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.

episage commented 6 years ago

+1

gustavosizilio commented 5 years ago

+1

estebgonza commented 5 years ago

+1

bradvogel commented 5 years ago

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);
});
SimeonC commented 4 years ago

I found there's a shorter version as end takes a callback.

await new Promise((resolve) => {
  writer.end(resolve);
});
vitaly-t commented 3 years ago

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.