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

前端 未结 7 513
遥遥无期
遥遥无期 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:35

    Being deprecated because it's an anti-pattern according to some. I.e. it's not safe to trust exists() and then doing something with the file because the file can be removed in between the exists-call and the doing-something-call.

    I agree in the above case. But to me, there is more use of exists(). I place empty dummy files in my temp- and cache directories. When I perform dangerous operations, such as removing old files from the cache dir I look for my dummy file to ensure that I'm not operating in the wrong directory. I.e. I just need to confirm that the file is there. Exists suits the bill perfectly for this, but I guess I'll switch to using stat() instead.

    0 讨论(0)
提交回复
热议问题