Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {     child.stdout.pipe(process.stdout); }); 

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

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

like image 880
David Avatar asked Mar 11 '14 21:03

David


People also ask

How do I wait in node JS?

One way to delay execution of a function in NodeJS is to use the seTimeout() function. Just put the code you want to delay in the callback. For example, below is how you can wait 1 second before executing some code.


1 Answers

Use exit event for the child process.

var child = require('child_process').exec('python celulas.py') child.stdout.pipe(process.stdout) child.on('exit', function() {   process.exit() }) 

PS: It's not really a duplicate, since you don't want to use sync code unless you really really need it.

like image 122
alex Avatar answered Sep 21 '22 15:09

alex