I have some code that I need to wrap with mine, the original code is creating a WriteStream
this way -
function createLog() {
this.stream = this.fs.createWriteStream(this.logFilePath, {flags: 'a'});
this.stream.write('log content');
this.stream.close();
}
function testLog(){
this.createLog();
// Here I want to wait until the file is closed
// something like this.stream.waitToBeClosed();
let text = fs.readFileSync(this.logFilePath, 'utf-8');
return text.match(
`log content`
);
}
then, in the end of the function the stream is closed using -
this.stream.close()
I need to use that function, and then examine the log file and verify it contain all the required data, but unfortunately sometimes the last lines are missing. I believe it's because the stream is still not closed when I open the file for reading.
I know that I can use stream.on('finish'...
to execute code when the stream is closed, but I am not supposed to touch the createLog code... (As I am writing a test for it) and before the execution of the createLog function I have no access to the stream
object.
Is there a way that I can wait
until the stream is closed from outside the original function?