Node: How to wait until a file is completed before processing it?
file, io, node.js
Solution
Do your file's post-processing in the `writeable stream's` `close` event:
outs.on('close', function() {
<<spawn your process>>
});
Also, no need for the `destroySoon` after the `end`, they are one and the same.
Problem
My Problem is that I can not be sure when a file has been successfully written to and the file has been closed. Consider the following case: ``` var fs = require('fs'); var outs = fs.createWriteStream('/tmp/file.txt'); var ins = <some input stream> ... ins.on('data', function(chunk) { out.write(chunk); }); ... ins.on('end', function() { outs.end(); outs.destroySoon(); fs.stat('/tmp/file.txt', function(err, info) { // PROBLEM: Here info.size will not match the actual number of bytes. // However if I wait for a few seconds and then call fs.stat the file has been flushed. }); }); ``` So, how do I force or otherwise ensure that the file has been properly flushed before accessing it? I need to be sure that the file is there and complete, as my code has to spawn an external process to read and process the file.