I am new to JS and I have been trying to fix this. Got some answers in SO but not exactly this. I am making an API that needs to call a Python script to calculate the area. The result shows up if I do a console.log() but I store it in a variable and console.log() it, then the value shows to be undefined. Is there any way I can store the answer in a variable to be used later on?
const { spawn } = require("child_process");
var result;
function res() {
const output = new Promise(function (resolve, reject) {
const pyprog = spawn("python", ["textArea.py"]);
pyprog.stdout.on("data", function (data) {
console.log(Number(data));
result = Number(data);
});
});
}
res();
console.log("the value in result is" + result);
Expected Output
24.2
the value in result is 24.2
Current Output
the value in result is undefined
24.2