Skip to content

Commit 80d6ec3

Browse files
committed
doc: Provide bad/better examples for access() and exists()
1 parent c21a212 commit 80d6ec3

File tree

1 file changed

+164
-5
lines changed

1 file changed

+164
-5
lines changed

doc/api/fs.md

+164-5
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,93 @@ fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => {
340340
});
341341
```
342342

343+
Using `fs.access()` to check for the accessibility of a file before calling
344+
`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing
345+
so introduces a race condition, since other processes may change the file's
346+
state between the two calls. Instead, user code should open/read/write the
347+
file directly and handle the error raised if the file is not accessible.
348+
349+
For example:
350+
351+
352+
**write (NOT RECOMMENDED)**
353+
354+
```js
355+
fs.access('myfile', (err) => {
356+
if (!err) {
357+
console.error('myfile already exists');
358+
return;
359+
}
360+
361+
fs.open('myfile', 'wx', (err, fd) => {
362+
if (err) throw err;
363+
writeMyData(fd);
364+
});
365+
});
366+
```
367+
368+
**write (RECOMMENDED)**
369+
370+
```js
371+
fs.open('myfile', 'wx', (err, fd) => {
372+
if (err) {
373+
if (err.code === "EEXIST") {
374+
console.error('myfile already exists');
375+
return;
376+
} else {
377+
throw err;
378+
}
379+
}
380+
381+
writeMyData(fd);
382+
});
383+
```
384+
385+
**read (NOT RECOMMENDED)**
386+
387+
```js
388+
fs.access('myfile', (err) => {
389+
if (err) {
390+
if (err.code === "ENOENT") {
391+
console.error('myfile does not exist');
392+
return;
393+
} else {
394+
throw err;
395+
}
396+
}
397+
398+
fs.open('myfile', 'r', (err, fd) => {
399+
if (err) throw err;
400+
readMyData(fd);
401+
});
402+
});
403+
```
404+
405+
**read (RECOMMENDED)**
406+
407+
```js
408+
fs.open('myfile', 'r', (err, fd) => {
409+
if (err) {
410+
if (err.code === "ENOENT") {
411+
console.error('myfile does not exist');
412+
return;
413+
} else {
414+
throw err;
415+
}
416+
}
417+
418+
readMyData(fd);
419+
});
420+
```
421+
422+
The "not recommended" examples above check for accessibility and then use the
423+
file; the "recommended" examples are better because they use the file directly
424+
and handle the error, if any.
425+
426+
In general, check for the accessibility of a file only if the file won’t be
427+
used directly, for example when its accessibility is a signal from another
428+
process.
429+
343430
## fs.accessSync(path[, mode])
344431
<!-- YAML
345432
added: v0.11.15
@@ -598,11 +685,83 @@ fs.exists('/etc/passwd', (exists) => {
598685
});
599686
```
600687

601-
`fs.exists()` should not be used to check if a file exists before calling
602-
`fs.open()`. Doing so introduces a race condition since other processes may
603-
change the file's state between the two calls. Instead, user code should
604-
call `fs.open()` directly and handle the error raised if the file is
605-
non-existent.
688+
Using `fs.exists()` to check for the existence of a file before calling
689+
`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing
690+
so introduces a race condition, since other processes may change the file's
691+
state between the two calls. Instead, user code should open/read/write the
692+
file directly and handle the error raised if the file does not exist.
693+
694+
For example:
695+
696+
**write (NOT RECOMMENDED)**
697+
698+
```js
699+
fs.exists('myfile', (exists) => {
700+
if (exists) {
701+
console.error('myfile already exists');
702+
} else {
703+
fs.open('myfile', 'wx', (err, fd) => {
704+
if (err) throw err;
705+
writeMyData(fd);
706+
});
707+
}
708+
});
709+
```
710+
711+
**write (RECOMMENDED)**
712+
713+
```js
714+
fs.open('myfile', 'wx', (err, fd) => {
715+
if (err) {
716+
if (err.code === "EEXIST") {
717+
console.error('myfile already exists');
718+
return;
719+
} else {
720+
throw err;
721+
}
722+
}
723+
writeMyData(fd);
724+
});
725+
```
726+
727+
**read (NOT RECOMMENDED)**
728+
729+
```js
730+
fs.exists('myfile', (exists) => {
731+
if (exists) {
732+
fs.open('myfile', 'r', (err, fd) => {
733+
readMyData(fd);
734+
});
735+
} else {
736+
console.error('myfile does not exist');
737+
}
738+
});
739+
```
740+
741+
**read (RECOMMENDED)**
742+
743+
```js
744+
fs.open('myfile', 'r', (err, fd) => {
745+
if (err) {
746+
if (err.code === "ENOENT") {
747+
console.error('myfile does not exist');
748+
return;
749+
} else {
750+
throw err;
751+
}
752+
} else {
753+
readMyData(fd);
754+
}
755+
});
756+
```
757+
758+
The "not recommended" examples above check for existence and then use the
759+
file; the "recommended" examples are better because they use the file directly
760+
and handle the error, if any.
761+
762+
In general, check for the existence of a file only if the file won’t be
763+
used directly, for example when its existence is a signal from another
764+
process.
606765

607766
## fs.existsSync(path)
608767
<!-- YAML

0 commit comments

Comments
 (0)