33
var sys = require('sys');
var exec = require('child_process').exec;
var cmd = 'whoami';
var child = exec( cmd,
      function (error, stdout, stderr) 
      {
        var username=stdout.replace('\r\n','');
      }
);

var username = ?

How can I find username outside from exec function ?

3
  • You can have the username outside of the exec function (ie in global or child scope) or you can have an object keep track of some scope variables Commented Oct 18, 2013 at 6:18
  • Is exec method asynchronous? Commented Oct 18, 2013 at 6:18
  • possible duplicate of Pass additional parameter to Javascript callback function Commented Jan 24, 2015 at 0:07

2 Answers 2

34

You can pass the exec function a callback. When the exec function determines the username, you invoke the callback with the username.

    var child = exec(cmd, function(error, stdout, stderr, callback) {
        var username = stdout.replace('\r\n','');
        callback( username );
    });


Due to the asynchronous nature of JavaScript, you can't do something like this:

    var username;

    var child = exec(cmd, function(error, stdout, stderr, callback) {
        username = stdout.replace('\r\n','');
    });

    child();

    console.log( username );

This is because the line console.log( username ); won't wait until the function above finished.


Explanation of callbacks:

    var getUserName = function( callback ) {            
        // get the username somehow
        var username = "Foo";    
        callback( username );
    };

    var saveUserInDatabase = function( username ) {
        console.log("User: " + username + " is saved successfully.")
    };

    getUserName( saveUserInDatabase ); // User: Foo is saved successfully.
Sign up to request clarification or add additional context in comments.

5 Comments

` callback(username); ^ TypeError: undefined is not a function at D:\wamp\www\keywords\ddd.js:7:5 at ChildProcess.exithandler (child_process.js:630:7) at ChildProcess.EventEmitter.emit (events.js:98:17) at maybeClose (child_process.js:730:16) at Socket.<anonymous> (child_process.js:943:11) at Socket.EventEmitter.emit (events.js:95:17) at Pipe.close (net.js:451:12)`
You have to define a function to pass as a callback.
with you first 4 lined code I am getting "undefiend is not a function" at 3rd line "callback(username)"
How can we define a function "child" as per 2nd commnet?
For people new to this, the first code block is really confusing without the wrapper that passes the callback function. Also, it seems the callback from exec receives three strings: nodejs.org/api/…
4

You can write the "exec" statement in a function that has a callback... Like This

var sys = require('sys');
var exec = require('child_process').exec;
var cmd = 'whoami';
function execChild(callback){
    var child = exec( cmd,
          function (error, stdout, stderr) 
          {
            username=stdout.replace('\r\n','');
             callback(username);
          }
 )};
    execChild(function(username){
    console.log(username);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.