51

Is there any stdout flush for nodejs just like python or other languages?

sys.stdout.write('some data')

sys.stdout.flush()

Right now I only saw process.stdout.write() for nodejs.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
TonyTakeshi
  • 5,869
  • 10
  • 51
  • 72

4 Answers4

24

process.stdout is a WritableStream object, and the method WritableStream.write() automatically flushes the stream (unless it was explicitly corked). However, it will return true if the flush was successful, and false if the kernel buffer was full and it can't write yet. If you need to write several times in succession, you should handle the drain event.

See the documentation for write.

Ben Blank
  • 54,908
  • 28
  • 127
  • 156
jonvuri
  • 5,738
  • 3
  • 24
  • 32
  • The documentation says that it returns `true` just if the data was added into the buffer and there is some free space in the buffer (total size of the buffer is less that `highWaterMark`). So it's at all not the same thing as flushing of data. Data is not even in the hard drive's cache, the data is just in the program's memory. Data is flashed when the `callback` is fired. The `drain` event just signals that a free space appeared in the buffer. – KeyKi Jun 13 '21 at 04:18
  • Yeah. This answer has been deprecated since late 2013. – KeyKi Jun 13 '21 at 04:21
10

In newer NodeJS versions, you can pass a callback to .write(), which will be called once the data is flushed:

sys.stdout.write('some data', () => {
  console.log('The data has been flushed');
});

This is exactly the same as checking .write() result and registering to the drain event:

let write = sys.stdout.write('some data');
if (!write) {
  sys.stdout.once('drain', () => {
    console.log('The data has been flushed');
  });
}
Syntle
  • 5,168
  • 3
  • 13
  • 34
jviotti
  • 17,881
  • 26
  • 89
  • 148
  • I would add that if you are doing this in response to an event stream, this approach only works properly if you can `pause()` if `!write` then `resume()` once `drain` fires, otherwise you just grow a huge backpressure pile regardless... – FremyCompany Apr 29 '19 at 12:46
  • 1
    With node 12 I use `process.stdout.write` instead – zella May 21 '19 at 12:32
3

write returns true if the data has been flushed. If it returns false, you can wait for the 'drain' event.

I think there is no flush, because that would be a blocking operation.

Ishtar
  • 11,542
  • 1
  • 25
  • 31
  • @Kiyura Almost right. Stream.write is not blocking but for stdout it usually is : http://nodejs.org/api/process.html#process_process_stdout Thanks, didn't know that. – Ishtar Sep 20 '12 at 10:47
  • I see. I didn't really know either way, to be honest, but I assumed that it would have to be blocking to at least the point that it wrote to the kernel buffer. I guess that's not "blocking enough" for it to count according to Node. :) – jonvuri Sep 20 '12 at 10:52
0

There is another function stdout which to clear last output to the terminal which is kind of work like flush

function flush() {
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
}

var total = 5000;
var current = 0;
var percent = 0;
var waitingTime = 500;
setInterval(function() {
    current += waitingTime;
    percent = Math.floor((current / total) * 100);
    flush();
    process.stdout.write(`downloading ... ${percent}%`);
    if (current >= total) {
        console.log("\nDone.");
        clearInterval(this);
    }
}, waitingTime);

cursorTo will move the cursor to position 0 which is the starting point

use the flush function before stdout.write because it will clear the screen, if you put after you will not see any output

Jeeva Kumar
  • 353
  • 2
  • 8
  • 4
    This clears and re-prints the current line. I think what they mean by "flush" is to immediately send any data waiting to be sent to stdout. If a program is very busy and you write something to stdout, it won't immediately get sent until it has spare time. `write` sends the data to a buffer that gets flushed when the program is free. – Ben J Jun 06 '18 at 07:33
  • @BenJ, to your description, even if the OP has that intention it is not clear. And moreover, this answer is what I was looking for with "flush"ing in mind; clear and rewrite on same page. so got my upvote – Yılmaz Durmaz Jan 10 '20 at 18:01
  • are you confusing with "flash" (to appear & disappear quickly, as in "flashing lights") vs "flush" (to be emptied or cleaned by a rapid flow of water, as a toilet) ? With respect to files and streams, "flushing" has a standard meaning of spilling out whatever output was waiting in a buffer. – Beni Cherniavsky-Paskin Mar 20 '22 at 15:03