I'm relatively new to javascript and I've run into the following issue which has been bugging me for a few hours now. I'm working with the following javascript and html file, but for some reason I can't seem to access the variables in test.js from ui.html
test.js
var ans;
function execShellCommand(cmd) {
const exec = require('child_process').exec;
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.warn(error);
}
resolve(stdout? stdout : stderr);
});
});
}
async function myasyncfuntion() {
ans = await execShellCommand('sh diffcheck.sh');
console.log(ans);
}
myasyncfuntion();
ui.html
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="test.js"></script>
</head>
<script>
const diffString = ans;
alert(diffString);
</script>
</html>
Can anyone explain to me why ans from test.js is not printed in the alert window when ui.html is run? And how can I fix it?