summaryrefslogtreecommitdiff
path: root/using_Q
diff options
context:
space:
mode:
authorPepper <pepper@scannerjammer.com>2014-05-07 14:12:04 -0400
committerPepper <pepper@scannerjammer.com>2014-05-07 14:12:04 -0400
commit236c7aa223c1a93bd59746699470288cc3f500a4 (patch)
treed86b9fceb624da0161d38c7de9dc5fca9c6b5c44 /using_Q
parent034c1bbdcc6730f6d185a341f809d89a5fd5fb05 (diff)
Diffstat (limited to 'using_Q')
-rw-r--r--using_Q/execute-cmd.js29
-rw-r--r--using_Q/ls-test-3.js14
2 files changed, 23 insertions, 20 deletions
diff --git a/using_Q/execute-cmd.js b/using_Q/execute-cmd.js
index 37b61eb..6acf9c9 100644
--- a/using_Q/execute-cmd.js
+++ b/using_Q/execute-cmd.js
@@ -3,21 +3,22 @@ var childProcess = require("child_process");
function dumper(content){
console.log(JSON.stringify(content));
}
-function execute(cmd){
- var deferred = Q.defer();
- var result = {};
- var sh = childProcess.exec(cmd, function(error, stdout, stderr){
- console.log(stdout)
- result.stdout = stdout;
+//okay...this works...I had to cut out sh.on...does this seem alright? hope so
+//when I use sh.on("exit",...) for whatever reason it isn't working, do you have an idea of why? shouldn't it be childProcess.on() instead? no
+function execute(cmd){
+ var deferred = Q.defer();
+ var result = {};
+ var sh = childProcess.exec(cmd, function(error, stdout, stderr){
+ result.stdout = stdout;
result.stderr = stderr;
result.error = error;
- deferred.resolve(result);
- });
-// sh.on("exit", function(){
-// console.log(result.stdout)
-// });
- return deferred.promise
+ });
+ sh.on("close", function(){
+ deferred.resolve(result);
+ });
+ return deferred.promise
}
-
execute("echo bitches").then(function(result){ dumper(result) });
-//DO NOT USE sh.on
+//DO NOT USE sh.on("exit"...
+//so instead of exit, "close" event. exit will just says that child exited, and i gues sonly after emitting this event
+//childproicess start to push data to stdout callback. great
diff --git a/using_Q/ls-test-3.js b/using_Q/ls-test-3.js
index 93ca560..2a57895 100644
--- a/using_Q/ls-test-3.js
+++ b/using_Q/ls-test-3.js
@@ -3,27 +3,29 @@ 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 promise_chain = Q.fcall(function(){ console.log("chain start") });
- var sync_operations = [ "ls | grep \"[^a-z]\"", "cat" ];
+ var sync_operations = [ "ls | grep \"[^a-z]\"", "echo a" ];
sync_operations.forEach(function(async_op) {
var promise_link = function() {
var deferred = Q.defer();
- child_process.exec(async_op, function(error, stdout, stderr) {
+ 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;
};
- // 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
+//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.