0

I'm new to javascript and can't get my head around asyn functions. As a hobbyist I'm making a web application in node.js, express. The server side is using a python-shell to execute some more advanced calculations.

After performing the calculations the python-shell returns roughly 1000 times a number which I would like to store in my javascript array (myArr). After finishing this process I would like to send my array to the client side (for illustration purposes).

The code below runs fine, meaning python returns the numbers one by one (checked by the console.log), and my javascript sends my array to the client side.

Unfortunately, my results array (myArr) is empty. This has probably something to do with the asyn function. Hopefully some one can help me out, How can I first fill myArr with data from the python-shell and afterwards rendering my page? Below are the relevant parts of my code.

var myArr = [];
var count = 0; 
var python = new PythonShell('majorPythonFile.py');
python.send(JSON.stringify([x1, x2,path, analyseType]));
python.on('message', function (message) {
    myArr[count] = message;
    count++;
    console.log(message + ' ' + count); 
});
# end the input stream and allow the process to exit
python.end(function(err){
    if (err){
        throw err;
    }
    console.log('finished');
});
res.render("home",{results:myArr});
Kirankumar Dafda
  • 2,354
  • 5
  • 29
  • 56
M. Punt
  • 13
  • 2

0 Answers0