1

​I am very new to nodejs. I have been playing around trying to implement a npm package node-cmd, execute the cmd and set the output equal to a variable. I have implemented the following:

var cmd=require('node-cmd');
var dir;
function test(data)
        {
         console.log("data content: " + data)
        };
var outputStuff = cmd.get('pwd',test);
console.log("outputStuff content: " + outputStuff);

output:

outputStuff content: undefined'
data content: /Users/jduff/Development/node/systemProperties

It is clear that the var "outputStuff" is set prior to executing the cmd.get function and returns 'undefined'. So my recollection from the course I took a while ago is that the outputStuff variable is being hoisted and set prior to executing the function. My question is how would I get the contents of the function to be set to the variable outputStuff.

console.log(outputStuff)

output:

outputStuff content: /Users/jduff/Development/node/systemProperties

Any insights to what I am missing will be greatly appreciated.

Aatif Akhter
  • 2,126
  • 1
  • 25
  • 46
John
  • 11
  • 1
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – JJJ Sep 15 '16 at 16:23
  • 1
    `node-cmd` is running async and calling the `test` callback upon completion of the `pwd` call. Since it's async, `outputStuff` isn't set. – Quotidian Sep 15 '16 at 16:23

0 Answers0