0

I need to run a couple of childprocesses and wait until they are done, and instead of using a long chain of callbacks I'm using Q promises. However what ever I try, the childproccesses are executed and Q just continues without waiting for the results.

var clones = repositores.map(function (project) {
    return Q.Promise(function (fullfill, reject) {
        require('child_process').exec('cd ' + clonepath + ' && git clone ' + project.repourl, function (err, stdout, stderr) {
            console.log(stdout);
            if (err) {
                console.log(stderr);
                reject(err);
            }
            else {
                fullfill();
            }
        });
    })
});

Q.all(clones).then(function(){
    console.log("Clones complete");
}).done();

This will give "Clone complete" and after comes the clone result.

I've tried with child_process.spawn, fork, exec, execFile with the same result both callbacks and event based.

I've also tried wrapping the Promises in a returned function.

9
  • You seem to want to wait until the stdout stream closes?
    – Bergi
    Commented Jan 19, 2016 at 15:04
  • I've tried that with a spawn process. spawn.on("close"), with the same result.
    – ztripez
    Commented Jan 19, 2016 at 15:13
  • Unless you are working with an older version of the API you want return Q.Promise... rather than return new Q.Promise...
    – bknights
    Commented Jan 19, 2016 at 15:44
  • Oh, must have missed that, thanks. Doesn't do anything for the result however..
    – ztripez
    Commented Jan 19, 2016 at 15:49
  • In first of all use Q.allSettled() instead of all() of course if you don't want to fail aggregated promise on the first failed command. also you may want to use "child-process-promise" wrapper instead of writing your own one.
    – zamuka
    Commented Jan 19, 2016 at 16:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.