diff options
Diffstat (limited to 'using_Q')
| -rw-r--r-- | using_Q/ls-test-2.js | 37 | ||||
| -rw-r--r-- | using_Q/ls-test-3.js | 29 | ||||
| -rw-r--r-- | using_Q/ls-test.js | 28 |
3 files changed, 94 insertions, 0 deletions
diff --git a/using_Q/ls-test-2.js b/using_Q/ls-test-2.js new file mode 100644 index 0000000..df71a72 --- /dev/null +++ b/using_Q/ls-test-2.js @@ -0,0 +1,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] diff --git a/using_Q/ls-test-3.js b/using_Q/ls-test-3.js new file mode 100644 index 0000000..93ca560 --- /dev/null +++ b/using_Q/ls-test-3.js @@ -0,0 +1,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 diff --git a/using_Q/ls-test.js b/using_Q/ls-test.js new file mode 100644 index 0000000..c90143d --- /dev/null +++ b/using_Q/ls-test.js @@ -0,0 +1,28 @@ +#!/usr/local/bin/node +Q = require('q'); +var childProcess = require('child_process'), + ls; + + +function get_the_async_data() { + var deferred = Q.defer(); + + ls = childProcess.exec('ls -l', 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); + }); + + return deferred.promise; +} +get_the_async_data().then(function(code) { console.log(code) } ); +//result should be '0', success! |
