summaryrefslogtreecommitdiff
path: root/using_Q/execute-cmd.js
blob: 6acf9c9541a14d8c39e8b1a6de0c4ff41def363b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var Q = require("q");
var childProcess = require("child_process");
function dumper(content){
  console.log(JSON.stringify(content));
}
//okay...this works...I had to cut out sh.on...does this seem alright? hope so
//when I use sh.on("exit",...) for whatever reason it isn't working, do you have an idea of why? shouldn't it be childProcess.on() instead? no
function execute(cmd){
  var deferred = Q.defer();
  var result = {};
  var sh = childProcess.exec(cmd, function(error, stdout, stderr){
    result.stdout = stdout;
    result.stderr = stderr;
    result.error = error;
  });
  sh.on("close", function(){                                                                                         
    deferred.resolve(result);    
  });
  return deferred.promise
}                                                                                                                              
execute("echo bitches").then(function(result){ dumper(result) });
//DO NOT USE sh.on("exit"...
//so instead of exit, "close" event. exit will just says that child exited, and i gues sonly after emitting this event
//childproicess start to push data to stdout callback. great