0

I'm just trying to add some text from a text file to an array. I can get all of the text to print individually, but I cannot seem to add it to the array. The code that I have now is:

const fs = require('fs');
let wordList = []

fs.readFile('answers.txt', 'utf-8', (err, data) => {
    if (err){
        console.log(err);
    }
    else{
        wordList.push(data);
    }
});

console.log(wordList);

For the final console.log, I was expecting a long array, but instead I get an empty array. The issue is with the push function, but aside from that, I do not know another way to add to an array.

Solvent
  • 1
  • 2
  • the function in `fs.readFile` is async – Liam Mar 25 '22 at 14:07
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Liam Mar 25 '22 at 14:07
  • Seems to be a duplicate of [this](https://stackoverflow.com/questions/18386361/read-a-file-in-node-js). – Tu Nguyen Mar 25 '22 at 14:11

0 Answers0