summaryrefslogtreecommitdiff
path: root/using_Q/ls-test-3.js
blob: 93ca560736f4ce21350acb35286194b6e6639ec0 (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
#!/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(){});

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

      // add the link onto the chain
      promise_chain = promise_chain.then(promise_link);
  });
  return promise_chain
}
go_big().then(function(result){ dumper(result) } )
//didn't quite get this one
//https://coderwall.com/p/ijy61g