title: Cannot return json object from a JavaScript function in a nodejs project that deals with a python script?
body: <p>I think that some asynchronous stuff is happening that I cannot figure out
  Please tell how can I solve this with <code>async-await</code> if possible.</p>
  <p>my code:</p>
  <pre><code>const {spawn} = require('child_process')
  
  
  const getWeatherReport = (location) =&gt; {
      let dataToSend, result
      const python = spawn('python', [__dirname+'\\weathergetter.py', location.toString()])
  
      python.stdout.on('data', (data) =&gt; {
          console.log('Pipe data from python script ...');
          dataToSend = JSON.parse(data.toString().replace(/\'/g, '\&quot;'));
          // console.log(dataToSend)
      })
      
      python.stderr.on('data', data =&gt; {
          console.error(`stderr: ${data}`)
      })
    
      python.on('close', (code) =&gt; {
          console.log(`child process close all stdio with code ${code}`);
          // console.log(dataToSend)
      })
      return dataToSend
      
  }
  
  var wea = getWeatherReport(&quot;kolkata&quot;)
  console.log(wea)
  </code></pre>
  <blockquote>
  <p><strong>Output</strong>: <br>
  undefined<br>
  Pipe data from python script ...<br>
  child process close all stdio with code 0<br></p>
  </blockquote>
  <p>I want the function to return a <code>json object</code> which is assigned to <code>dataToSend</code>. Here I am actually working with a python script.</p>
  <p>the python script has no problem. When I <code>console.log(dataToSend)</code> inside <code>python.stdout.on()</code> and <code>python.on()</code> as <strong>commented out in the code</strong>, the json objects gets printed to the console. But I cannot <strong>make the function return the json object</strong>.</p>
  <h2>Please refer to these for what I was following:</h2>
  <ul>
  <li><strong>mainly this one</strong> -- <a href="https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f" rel="nofollow noreferrer">https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f</a></li>
  <li><a href="https://www.sohamkamani.com/blog/2015/08/21/python-nodejs-comm/" rel="nofollow noreferrer">https://www.sohamkamani.com/blog/2015/08/21/python-nodejs-comm/</a></li>
  <li><a href="https://towardsdatascience.com/python-pandas-in-node-js-application-47a76ae42a2a" rel="nofollow noreferrer">https://towardsdatascience.com/python-pandas-in-node-js-application-47a76ae42a2a</a></li>
  </ul>
  <p><em><strong>NOTE: Please don't mark this question as duplicate as it was done to my last question. I can understand that there are many posts on this topic in stack overflow already. But it is not helping in my case.</strong></em></p>
  <h2>What I tried after my previous post was closed:</h2>
  <pre><code>const {spawn} = require('child_process')
  
  
  const getWeatherReport = async (location) =&gt; {
      let dataToSend
      const python = spawn('python', [__dirname+'\\weathergetter.py', location.toString()])
  
      python.stdout.on('data', async (data) =&gt; {
          console.log('Pipe data from python script ...');
          dataToSend = JSON.parse(data.toString().replace(/\'/g, '\&quot;'));
          // console.log(dataToSend)
      })
      
      python.stderr.on('data', data =&gt; {
          console.error(`stderr: ${data}`)
      })
    
      python.on('close', (code) =&gt; {
          console.log(`child process close all stdio with code ${code}`);
          // console.log(dataToSend)
      })
      return  await dataToSend
      
  }
  
  var wea = await getWeatherReport(&quot;kolkata&quot;)
  console.log(`data: ${ await wea}`)
  </code></pre>