-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
doc: update examples of the final "callback" argument for fs.access() #20460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a8c52ec
a54e659
9ee4767
7bb85fc
59ad1f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
||
| ```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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| fs.access('./package.json', fs.constants.R_OK, (err) => { | ||
| console.log(err ? 'package.json could not be read' : 'package.json read'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vsemozhetbyt Yes, I forgot the Nit: |
||
| }); | ||
|
|
||
| // 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' : | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| 'package.json could be written to' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| ); | ||
| }); | ||
|
|
||
| // Check if `package.json` exists in the current directory and it could be | ||
| // written to. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with the comment: |
||
| 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'); | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nits:
and/or->andcould be written to->is writable