Stdout flush for NodeJS?

前端 未结 2 502
攒了一身酷
攒了一身酷 2021-02-11 16:26

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

2条回答
  •  一个人的身影
    2021-02-11 17:03

    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');
      });
    }
    

提交回复
热议问题