diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2013-10-05 14:45:10 +0200 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2013-10-05 14:54:57 +0200 |
commit | d97ea06d885c6c614bbe1d073cc9167cb66fd564 (patch) | |
tree | bfdb22a9ca43f95468c844e96507ddc317558d6b | |
parent | b7f36e187d1b5362a72b50779aff182d7131d8f5 (diff) | |
download | node-new-d97ea06d885c6c614bbe1d073cc9167cb66fd564.tar.gz |
doc: add warning to fs.exists() documentation
Warn against the open-if-exists anti-pattern, it's susceptible to
race conditions.
-rw-r--r-- | doc/api/fs.markdown | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/doc/api/fs.markdown b/doc/api/fs.markdown index 129f3c5767..f940e6364f 100644 --- a/doc/api/fs.markdown +++ b/doc/api/fs.markdown @@ -595,6 +595,13 @@ Then call the `callback` argument with either true or false. Example: util.debug(exists ? "it's there" : "no passwd!"); }); +`fs.exists()` is an anachronism and exists only for historical reasons. +There should almost never be a reason to use it in your own code. + +In particular, checking if a file exists before opening it is an anti-pattern +that leaves you vulnerable to race conditions: another process may remove the +file between the calls to `fs.exists()` and `fs.open()`. Just open the file +and handle the error when it's not there. ## fs.existsSync(path) |