0

I'm having an issue returning a value after determining something from fs.stat(), here is the example below:

function checkFile(fileName, type) {

   var status = false;

   // Get the stats assocatied to this file
   fs.stat(fileName, function (error, stats) {

      // get the time when the file was last modified
      var fileTime = moment(stats.mtime);

      if(type === "behind") {

        var query = moment(main.repoTime).isAfter(fileTime);

      } else if(type === "updated") {

        var query = moment(fileTime).isAfter(main.repoTime);

      }

      status = query;

    });

    return status;

}

What am I exactly doing wrong here? If I took fs.stat it works fine?

Michael Wilson
  • 1,548
  • 3
  • 21
  • 44
  • 2
    Asynchronous call means no ability to return. What you are doing is is like ordering a pizza over the phone and eating it as soon as you hang up. Not possible because you need to wait for it to be delivered. – epascarello Jul 16 '15 at 16:51
  • 2
    `checkFile` should accept a callback it calls with the result, like nearly all NodeJS functions do. As a very, very much second-class option, use `fs.statSync`. But it's much better to embrace the asynchronous nature of things when doing Node programming... – T.J. Crowder Jul 16 '15 at 16:52

0 Answers0