我启用了父进程和子进程之间的通信,以便按如下方式发送JSON:
儿童:
try {
var price1 = parseInt(process.argv[2]);
if (!price1) {
throw new Error('Price in calculations.js undefined');
}
var result = {
'timeStamp' : Date(),
'prices' : { 'player1' : price1, 'player2' : 666}
};
process.send(result);
} catch (e) {
// In case of an error, I get here as expected.
process.send(e);
}
Run Code Online (Sandbox Code Playgroud)
家长:
var spawn = require('child_process').spawn;
var child = spawn('node', ['calculations.js', 333], {stdio: [null,null,'pipe','ipc']});
child.on('message', function(data) {
if (data instanceof Error) {
// In case of an error, …Run Code Online (Sandbox Code Playgroud) 是否可以根据条件在流中键入变量?像这样的东西:
const type = 'xyz';
const a: (type === 'xyz') ? number : string;
Run Code Online (Sandbox Code Playgroud)