Friday 2 June 2017

How to wait before file stream is done writing before I close it?

I create a file stream to write a csv file:

    var logger = fs.createWriteStream('restaurants.csv', {
      flags: 'w' // 'a' means appending (old data will be preserved)
    });
    logger.write("sep=,\n");

When I'm done writing, I close the stream like this:

    logger.end();
    logger.close();

But when I do that, part of the file is missing at the end. I tried to sleep before closing the stream:

    await Promise.delay(2000);
    logger.end();
    logger.close();

And suddenly, the file is complete. But timeout is kinda stupid solution. Does filestream emit any event when it's done writing. Basically what I need is:

    await logger.doneWriting();
    logger.end();
    logger.close();

I can promisify any event of course, but which event it is?



via Tomáš Zato

No comments:

Post a Comment