summaryrefslogtreecommitdiff
path: root/using_Q/ls-test-3.js
blob: 2a578955a01bd9245df89738e0cccd9558915acb (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
#!/usr/local/bin/node
Q = require('q');
var childProcess = require('child_process'), ls;
function dumper(content){
  console.log(JSON.stringify(content));
}          

function go_big(){
  var promise_chain = Q.fcall(function(){ console.log("chain start") });

  var sync_operations = [ "ls | grep \"[^a-z]\"", "echo a" ];
  sync_operations.forEach(function(async_op) {
      var promise_link = function() {
          var deferred = Q.defer();
          childProcess.exec(async_op, function(error, stdout, stderr) {
            var result = { "error" : error, "stdout" : stdout, "stderr" : stderr };  
            deferred.resolve(result);
          });
          //sh.on("close", function(){
          //});
          return deferred.promise;
      };

      promise_chain = promise_chain.then(promise_link);
  });
  return promise_chain
}
go_big().then(function(result){ dumper(result) } )
//https://coderwall.com/p/ijy61g
//oh right, it will return only last result, if you want all of them, need to pass something like results hash and
//push new results inside.