#!/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]