-3

The following Node.js code prints 'undefined', even though it found the file.

var fileFound = function() {
    fs.readFile('public/images/acphotos/Friedrich-EL36N35B.jpg', function(err, data) {
          if (err) {
            console.log(err);
            return false;
          } else {
            return true;
        }
    });
}

console.log("Return value: " + fileFound());

How would I rewrite it? I don't fully understand the solution in the other thread I was shown.

1 Answers1

1

Because the return statements are inside the callback passed into fs.readFile.

the fileFound function never returns anything, therefore you get undefined.

Michele Ricciardi
  • 2,107
  • 14
  • 17