nodejs child_process 范围问题

nodejs child_process scope issue

我正在使用 nodejs 的 child_process(fork) 然后我将数据发送到特定文件并接收数据但在接收到进程执行后不控制该数据

first.js

var cp = require('child_process');
var child = cp.fork('./s/abc.js');
child.send({data:'this come from parent process'});
var x ;

child.on('message', function(m) {
console.log(m); // output:- this come from child process
x = m;
});
console.log(m);
console.log(x);

abc.js (./s/abc.js)

process.send({data:'this come from child process'});
var x ;

process.on('message', function(m) {
console.log(m); // output:- this come from parent process
});

在作用域结束后使用函数控制值

first.js

var cp = require('child_process');
var child = cp.fork('./s/abc.js');
child.send({data:'this come from parent process'});
var x ;

child.on('message', function(m) {
console.log(m); // output:- this come from child process
callTempFun(m);
});
function callTempFun(m){
console.log(m); // output:- this come from child process
}

abc.js (./s/abc.js)

process.send({data:'this come from child process'});
var x ;

process.on('message', function(m) {
console.log(m); // output:- this come from parent process
});