process.exit(0): output disappears?

一个人想着一个人 提交于 2019-12-01 01:10:30
feklee

Phew, I figured it out myself. First of all, I discovered that a delay causes the output to appear:

hello_3.js:

console.log('Hello world!');
setTimeout(function () {
    process.exit(5);
}, 1000);

Output:

Hello world!
5

So I double checked Node.js documentation for console, and found:

The console functions are synchronous when the destination is a terminal or a file (to avoid lost messages in case of premature exit) and asynchronous when it's a pipe (to avoid blocking for long periods of time).

Then I decided to make sure that no pipe is used and wrote a batch script hello_2.bat:

@ECHO OFF
node hello_2.js >test
TYPE test

Output when calling the script with (call-process "cmd.exe" nil t nil "/C" "hello_2.bat"):

Hello world!
0

(return value is 0 instead of 5, but I don't care about that)

To answer my question:

  1. It looks like call-process in Emacs on Windows uses a pipe for retrieving output from programs.

    As I also mentioned EShell: It does not seem to be recognized as a terminal by Node.js on Windows, and possibly EShell internally uses call-process or similar to run programs.

  2. Detection of a pipe as destination for standard output causes console.log to be run asynchronously.

  3. process.exit(5) in Node.js on Windows seems to discard all scheduled asynchronous tasks, and thus no output is generated.

    This assumption is supported by the outcome of desperately directing output to a pipe inside the Windows command prompt:

    C:\Temp> node hello_2.js | MORE
    
    C:\Temp>
    

Finally, I found out that the issue is known since about a year ago (as of September 2013).

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