1

I wrote a simple module that reads a file called readHistory.js:

const fs = require('fs');


async function readHistory(history){
    fs.readFile("history_file.txt", function read(err, data) {
        if (err) {
            throw err;
        }

    const myRAM = data.toString();
    history = myRAM;
    //console.log(myRAM); 
    });
};
readHistory();
module.exports = { readHistory }

I import it to a different module like so:

const { readHistory } = require("./readHistory")

Then I try to get the value like this:

const contextClues = Promise.resolve(readHistory(myRAM));

Which returns:

 [object Promise]

I also tried like this:

const contextClues = Promise.resolve(readHistory(myRAM));    
contextClues.then((value) => {
    console.log("Other context: " + value)
})

Which returned Undefined.

I've tried a bunch of different permutations on the above so I'm starting to think the problem is in ReadHistory.js.

I've tried putting my expression inside an async function and using await:

const contextClues = await Promise.resolve(readHistory(myRAM))

And that returns Undefined which I think is worse. I've tried writing the function with and without an argument and it doesn't seem to matter. To be honest the argument is an artifact from when I tried writing it as a callback but that didn't work either.

Z-Man Jones
  • 187
  • 1
  • 12

1 Answers1

1

Moving a reading of a file to a separate module could be simplified with 1 line:

const fs = require('fs');
const history = fs.readFileSync('history_file.txt').toString();
Alexander Nenashev
  • 8,775
  • 2
  • 6
  • 17