user3246167
user3246167

Reputation: 113

writeSync() only writes to the console when console.log is present (node.js)

Take the following snippet:

try {
      fs = require('fs');
      fs.writeSync(0, 'Trying now...');
      fs.writeSync(0, 'worked!\r');
    }
catch(error){}

As is, it will not output to the console, however

  try {
  fs = require('fs');
  fs.writeSync(0, 'Trying now...');
  fs.writeSync(0, 'worked!\r');
  console.log();
}
catch(error){}

Will output "Trying now... worked!" to the console. What exactly is going on here?

Upvotes: 1

Views: 311

Answers (2)

Bergi
Bergi

Reputation: 665456

You're writing to the file descriptor but are not flushing it. Writing a line break (\n instead of \r) does lead to stdout flushing its buffer, as well as a console.log() call that forces it.

Upvotes: 3

user3246167
user3246167

Reputation: 113

Just realized it was because I was using \r instead of \n

Upvotes: 0

Related Questions