0

The below function, startScript, contains blocking code that takes some time to run. In my setInterval loop, I want to access the weight value that it returns. However, when I run this Node program I receive an error saying that new_weight is undefined.

I tried running startScript.on('close', ...) within the loop, but this throws an error saying that there is no method on for startScript

What am I doing wrong here?

var spawn = require('child_process').spawn
var path = require('path');
var split = require('split');
var pythonData = [];
var weight = null;

function startScript(){
  var pyScript = spawn('python', [ path.join(__dirname, 'script.py') ]);
  pyScript.stdout.on('data', function(lineChunk){
    pythonData = lineChunk.toString().replace(/[\S|\n|\[|\]]/,"").split(',');
  });

  pyScript.on('close', function(code){
    var sum = 0;
    for(var i=0; i < pythonData.length; i++){
      sum += parseFloat(pythonData[i]);
    }
    var weight = sum / pythonData.length;
    console.log("weight: " + weight);
    return weight;
  });
}

setInterval(function(){
  if (some event that occurs infrequently){
    startScript();
    var new_weight = weight + 100
    console.log(new_weight);
  }
}, 1000);
9
  • you never defined weight. and you never accepted a return value from start script.
    – Kevin B
    Commented Apr 18, 2014 at 18:27
  • I mean this in the nicest way possible but this example makes absolutely no sense. I'm assuming you've tried to abstract out a simple idea from a larger body of code. Is there any way you could give us the real code? Commented Apr 18, 2014 at 18:27
  • Ok, you defined weight, but you never gave it a value.
    – Kevin B
    Commented Apr 18, 2014 at 18:28
  • It's still just null. if startScript is giving weight a value, then you would be able to access it the way you are. jsfiddle.net/86z3Z
    – Kevin B
    Commented Apr 18, 2014 at 18:30
  • 1
    we need to see more of startScript. if startScript is asynchronous (which i believe to be the case given what you've said thus far) it can't return the value and you must use callbacks.
    – Kevin B
    Commented Apr 18, 2014 at 18:39

1 Answer 1

1

You cant' return from pyScript.on(), it's asynchronous. The parent function has returned already long before the other return happens. Instead, you must use callbacks.

function startScript(callback){ // ******
  var pyScript = spawn('python', [ path.join(__dirname, 'script.py') ]);
  pyScript.stdout.on('data', function(lineChunk){
    pythonData = lineChunk.toString().replace(/[\S|\n|\[|\]]/,"").split(',');
  });

  pyScript.on('close', function(code){
    var sum = 0;
    for(var i=0; i < pythonData.length; i++){
      sum += parseFloat(pythonData[i]);
    }
    var weight = sum / pythonData.length;
    console.log("weight: " + weight);
    //return weight; // you can't return weight. Instead, execute the callback.
    callback(weight); // ******
  });
}

setInterval(function(){
  if (some event that occurs infrequently){
    startScript(function(weight){ // ******
      var new_weight = weight + 100
      console.log(new_weight);
    }); // ******
  }
}, 1000);
0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.