fs.exists, fs.existsSync - why are they deprecated?

前端 未结 7 601
遥遥无期
遥遥无期 2021-01-07 20:06

I noticed that the official node documentation says something startling about fs.exists:

\"fs.exists() is an anachronism and exists only

7条回答
  •  忘掉有多难
    2021-01-07 20:29

    existsSync's implementation is like this (v0.10.25):

    function (path) {
      try {
        nullCheck(path);
        binding.stat(pathModule._makeLong(path));
        return true;
      } catch (e) {
        return false;
      }
    }
    

    so if you write program like if(fs.existsSync(thepath)){}, it is better to change it to if(fs.statSync(thepath)){}.

提交回复
热议问题