How does node child_process.exec open an exe, to know the result of the command immediately instead of waiting for the exe to exit

for example, open an exe program with child_process.exec. After opening it, the node program is useless. I want to close the node program immediately, but the callback function of exec must wait for the return value of the exit of the exe program. Is there any way to achieve the effect of the ShellExecute function in VC? Thank you.

Mar.24,2021

use spawn , see official document . For example:

Program source files to be executed by js:

// main.cc
-sharpinclude <iostream>
-sharpinclude <fstream>
-sharpinclude <windows.h>

using namespace std;

int main() {
  ofstream fs;
  Sleep(5000);
  fs.open("out.txt");
  cout << "Rua!" << endl;
  fs << "Rua!" << endl;
  fs.close();
  return 0;                                                        }

there are also js files:

// test.js
const { spawn } = require('child_process')

let subprocess = spawn('main.exe', [], {
  detached: true,
  stdio: 'inherit' // ['ignore', process.stdout, process.stderr]
})

subprocess.unref()
console.log('Bye')
process.exit()

but I haven't found a way to keep the console output for the time being. Of course, you can also execute a bat, to open a new window in bat

.
Menu