title: Node.js: Writing a function to return spawn stdout as a string
body: <p>I'm trying to return the output of this function as a string, but it keeps returning as undefined. Where am I going wrong?</p>
  
  <pre><code>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'));
  </code></pre>