javascript - Kill child process at the end of promise -


i use following code create child process, process working expected, want @ end of process kill , try last (which if put bp stops there after process done) question how kill , avoid error.

getcmd provide command run , working expected

var childprocess = promise.promisify(require('child_process').exec);  ....             .then(getcmd)             .then(childprocess)             .spread(function (stdout, stderr) {                 console.log(stdout, stderr);                 return stdout;             }).then(function(){                  childprocess.kill()             }) 

when line childprocess.kill() executed got error :

[typeerror: undefined not function] 

how overcome issue , kill process @ end

as far can tell, promisifying require('child_process').exec not correct. @ best, end function returns promise would, straightforwardly, denied reference child-process itself. imagination required.

you can call require('child_process').exec(cmd, callback) in normal way, inside new promise() wrapper and, critically, resolve/reject in such way reference child-process made available down promise chain.

i'm not sure bluebird's .promisify() quite flexible enough job here's manual promisification.

first couple or requires :

var promise = require('bluebird'); var child_process = require('child_process'); 

now manual promisifier :

function childprocesspromise (method, cmd) {     return new promise(function(resolve, reject) {         var child = child_process[method](cmd, function(error, stdout, stderr) {             if(error !== null) {                 error.child = child;//monkeypatch error custom .child property.                 reject(error);             } else {                 resolve({ 'child':child, 'stdout':stdout, 'stderr':stderr });//all available data bundled single object.             }         });     }); } 

as written promisifier flexible enough cater various child_process methods. pass (or bind) 'spawn', 'exec', 'execfile' or 'fork' first parameter.

your promise chain :

....     .then(getcmd)     .then(childprocesspromise.bind(null, 'exec'))     .then(function(result) {         // result.child, result.stdout , result.stderr available here.     }, function(error) {         //error available here, including custom .child property.     }); 

all untested, prepared adapt/fix.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -