David David - 1 year ago 815
Node.js Question

How to wait for a child process to finish in Node.js?

I'm running a Python script through a child process in Node.js, like this:

require('child_process').exec('python celulas.py', function (error, stdout, stderr) {

but Node doesn't wait for it to finish. How can I wait for the process to finish?

EDIT: Is it possible to do this by running the child process in a module I call from the main script?

Answer Source

You should use exec-sync

That allow your script to wait that you exec is done

really easy to use:

var execSync = require('exec-sync');

var user = execSync('python celulas.py');

Take a look at: https://www.npmjs.org/package/exec-sync

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download