1

I'm newbie at JS, and I didn't understand a thing. The problem is, I have a fs.readfile function, and there is while(1){//code} code execution bottom of it.

The example:

const prompt = require('prompt-sync')();
let fs = require('fs');

function aFuncName() {
  let nms;
  fs.readFileSync('filename.txt', (error, data) => {
    console.log('readfile began!')
    if (error)
      console.log(error);

    try {
      nms = data.toString().match(/\n/).length
        console.log('ind:' + String(ind))
    } catch (TypeError) {
      nms = 0;
    }
    console.log("readfile over!");
  });

  let userarry = [nms]
  while (1) {
    let vars = prompt('varsm):')
  }
}

the problem is, the function passes the fs.readfilesync part and begins from let userarry. I also tried fs.readFile

derpirscher
  • 14,418
  • 3
  • 18
  • 35
solac34
  • 156
  • 11
  • 1
    [`readFileSync`](https://nodejs.org/api/fs.html#fsreadfilesyncpath-options) does not take a callback function but returns the file content directly. And if you change to [`readFile`](https://nodejs.org/api/fs.html#fsreadfilepath-options-callback) read https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call and https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – derpirscher Dec 23 '22 at 16:22

0 Answers0