I write a readable stream to a file and want to know when it is finished so I can access the file on the disk.
I listen to the finish
event but when I do a stat
in the listener the file size is 0. After some delay the size is correct.
Is this normal behavior?
According to the docs the event is emitted when all data has been flushed to the underlying system.
var ws = fs.createWriteStream(path);
file.pipe(ws); // file is a readable stream
ws.on('finish', function() {
try {
stat = fs.statSync(path); // stat.size: 0
}
catch (err) { /*...*/ }
});
The finish
event is emitted when all data is written to the stream but file may not be closed at that time. You can listen the close
event which is emitted once the file handler is closed.
ws.once('close', function() {
try {
stat = fs.statSync(path);
}
catch (err) { /*...*/ }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With