Skip to content

Commit

Permalink
doc: update description of fs.exists in fs.markdown
Browse files Browse the repository at this point in the history
per: nodejs/node-v0.x-archive#7944

originally submitted by @oconnore

Minor update to the description in `fs.exists()`

Reviewed By: Sakthipriyan Vairamani <[email protected]>
PR-URL: #2378
  • Loading branch information
jasnell authored and rvagg committed Aug 26, 2015
1 parent 52018e7 commit 27c07b3
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -680,15 +680,11 @@ Then call the `callback` argument with either true or false. Example:
console.log(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.exists()` should not be used to check if a file exists before calling
`fs.open()`. Doing so introduces a race condition since other processes may
change the file's state between the two calls. Instead, user code should
call `fs.open()` directly and handle the error raised if the file is
non-existent.

## fs.existsSync(path)

Expand Down

0 comments on commit 27c07b3

Please sign in to comment.