title: JavaScript How to make a node wait before returning in node.js/Node-RED?
body: <p>experienced programmer, inexperienced javascript programmer.</p>
  
  <p>I am working with Node-RED and in one of my nodes, I am calling an external program with 'child_process' which sends data back to my node via stdout.</p>
  
  <p>I am using console.info(stdout) to log the child process data, so I know that it is working appropriately.  This is the code within the function node:</p>
  
  <pre><code>/* required modules */
  var cp = context.global.child_process;
  
  /* variables */
  var packet = {};
  var cmd = '~/packet/packet.py ';
  
  /* construct the packet object */
  packet.networkTime = (msg.payload[3] &lt;&lt; 24) + (msg.payload[2] &lt;&lt; 16) + (msg.payload[1] &lt;&lt; 8) + msg.payload[0];
  packet.sampleCount = msg.payload[4];
  packet.uncompressedWidth = msg.payload[5];
  packet.compressedWidth = msg.payload[6];
  
  packet.sample0 = (msg.payload[8] &lt;&lt; 8) + msg.payload[7];
  
  var compressedData = msg.payload.slice(9);
  
  /* change buffer type to array */
  packet.compressedData = [];
  compressedData.forEach(
      function(element){
          packet.compressedData.push(element);
      }    
  );
  
  /* stringify the object */
  var packet_str = "'" + JSON.stringify(packet) + "'";
  
  /* this section will execute the command */
  cp.exec(
      cmd + packet_str,
      function(error, stdout, stderr){
          console.info(stdout);
          return JSON.parse(stdout);
      }
  );
  
  //return data_msg;
  </code></pre>
  
  <p>I tried setting data_msg to {} and using JSON.parse(stdout) to place into that object, but the 'return data_msg' always executes before the value is returned.  I simply want to wait for my external process to return data before returning this node.</p>
  
  <p>Thank you for your assistance,</p>