I'm trying to return the output of this function as a string, but it keeps returning as undefined. Where am I going wrong?
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'));