Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
doc: update examples of the final "callback" argument for fs.access()
  • Loading branch information
BeniCheni committed May 2, 2018
commit a8c52ecf7a09fa3d9d96598b511530616342db49
41 changes: 37 additions & 4 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,45 @@ no effect on Windows (will behave like `fs.constants.F_OK`).

The final argument, `callback`, is a callback function that is invoked with
a possible error argument. If any of the accessibility checks fail, the error
argument will be an `Error` object. The following example checks if the file
`/etc/passwd` can be read and written by the current process.
argument will be an `Error` object. The following examples check if
`package.json` exists, and/or if it could be written to.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits:

  • and/or -> and
  • could be written to -> is writable


```js
fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => {
console.log(err ? 'no access!' : 'can read/write');
// Check if `package.json` exists in the current directory.
fs.access('./package.json', fs.constants.F_OK, (err) => {
console.log(err ? 'package.json does not exist' : 'package.json exists');
});

// Check if package.json file could be read.
Copy link
Copy Markdown
Contributor

@vsemozhetbyt vsemozhetbyt May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`package.json` for consistency?

fs.access('./package.json', fs.constants.R_OK, (err) => {
console.log(err ? 'package.json could not be read' : 'package.json read');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could not be read -> is readable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is not readable / is readable?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vsemozhetbyt Yes, I forgot the not. Should be:

Nit: could not be read -> is not readable

});

// Check if `package.json` could be written to.
fs.access('./package.json', fs.constants.W_OK, (err) => {
console.log(
err ?
'package.json could not be written to' :
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could not be written to -> is not writable

'package.json could be written to'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could be written to -> is writable

);
});

// Check if `package.json` exists in the current directory and it could be
// written to.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with the comment: could be written to -> is writable

fs.access('./package.json', fs.constants.F_OK | fs.constants.W_OK, (err) => {
// If there is an error, print the error in console and exit.
const isNonExistenceError = err && err.code === 'ENOENT';
const isReadOnlyError = err && err.code === 'EPERM';

if (isNonExistenceError || isReadOnlyError) {
console.error(
isNonExistenceError ?
'package.json does not exist' :
'package.json is read-only'
);
} else {
console.log('package.json exists, and it could be written to');
}
});
```

Expand Down