vianney
vianney

Reputation: 380

How can i pass argument to child_process.exec callback

is there a way to pass extra arguments to the callback function when i use child_process.exec(cmd,callback) ?

According to the documentation, the callback function only receive error,stdout,sterr.

I could eventually have an unix script who gets extra args, runs the command, and outputs result of the command and args to stdout but maybe there is a better way to do this

Thanks

Upvotes: 6

Views: 10782

Answers (2)

vianney
vianney

Reputation: 380

At the end, i have a function my_exec :

var exec = require('child_process').exec
function my_exec(cmd,data,callback)
{
    exec(cmd,function(err,stdout,stderr){
        callback(err,stdout,stderr,data)
    })
}

thank you!

Upvotes: 1

Noah
Noah

Reputation: 34303

You can call another function inside the exec callback

var exec = require('child_process').exec
function(data, callback) {
  var cmd = 'ls'
  exec(cmd, function (err, stdout, stderr) {
    // call extraArgs with the "data" param and a callback as well
    extraArgs(err, stdout, stderr, data, callback) 
  })
}

function extraArgs(err, stdout, stderr, data, callback) {
  // do something interesting
}

Upvotes: 6

Related Questions