我试图将此函数的输出作为字符串返回,但它一直以未定义的形式返回。我哪里错了?
function run(cmd){
var spawn = require('child_process').spawn;
var command = spawn(cmd);
var result = '';
command.stdout.on('data', function(data) {
result += data.toString();
});
command.on('close', function(code) {
return result;
});
}
console.log(run('ls'));