I'm new to JavaScript/NodeJS. I have the function getLevel()
which reads from a text file and outputs the result to the variable level
. My goal is to be able to use level outside of the getLevel()
function.
var fs = require("fs");
var AppData = process.env.APPDATA;
const readline = require('readline');
function getLevel() {
const rl = readline.createInterface({
input: fs.createReadStream(AppData + "\\pico-8\\carts\\level.txt"),
output: process.stdout,
terminal: false
});
rl.on('line', (line) => {
level = (line + "00m");
})
return(level);
};
getLevel();
level = getLevel();
console.log(level + " is the current level.");
I've tried googling to no help. Using let
or not using var
didn't help. If there's a better method to do what I'm trying to do I am open to that as well. Whatever works.