How can I write blocking in stdout with node.js?

◇◆丶佛笑我妖孽 提交于 2019-12-01 19:35:16

If you really really want synchronous writes to stdout you can do:

var fs = require('fs');
fs.writeSync(1, "Foo\n");
fs.fsyncSync(1);
thejh

Write using process.stdout.write, the return value is whether data got buffered. If it's true, continue writing when process.stdout emits the drain event.

If you want your code to look sync, use streamlinejs as described here: Node.js stdout flush

aredridel

Don't.

What you want to do is pause() your input when the output is full, like the pump() method does, then resume() it when you've got space to write. If not, your process balloons out to gargantuan size.

You probably want to use the more direct outputStream stuff for that, though, or the write() call, not console.log().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!