0

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.

jox
  • 2,218
  • 22
  • 32
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – evolutionxbox Jan 17 '21 at 13:19
  • @Alex No, you can't - only if it did return a promise, which it doesn't. – Bergi Jan 17 '21 at 13:36

0 Answers0