summaryrefslogtreecommitdiff
path: root/using_Q/ls-test-2.js
blob: df71a72ab0547ecaea70d66336804114cab6e37d (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
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/local/bin/node
Q = require('q');
var childProcess = require('child_process'), ls;
function dumper(content){
  console.log(JSON.stringify(content));
}

var things = [ "ls | grep \"[^a-z]\"", "ls /", "ls | while read a; do echo \"$a\" | sed s/\./MAMAMIA/g" ];

function get_all_the_things(things) {
    var the_promises = [];

    things.forEach(function(thing) {
        var deferred = Q.defer();

      ls = childProcess.exec(thing, function (error, stdout, stderr) {
        if (error) {
  //        console.log(error.stack);
  //        console.log('Error code: '+error.code);
  //        console.log('Signal received: '+error.signal);
        }
  //      console.log('Child Process STDOUT: '+stdout);
  //      console.log('Child Process STDERR: '+stderr);
      });

      ls.on('exit', function (code) {
  //      console.log('Child process exited with exit code '+code);
        deferred.resolve(code);
      });

        the_promises.push(deferred.promise);
    });

    return Q.all(the_promises);
}
get_all_the_things(things).then(function(result){ dumper(result) });
//[0,0,2]