How to use the access function from fs-promise

Find comprehensive JavaScript fs-promise.access code examples handpicked from public code repositorys.

The fs-promise.access function checks if a file or directory exists and the calling code has the required permissions to access it, and returns a promise.

719
720
721
722
723
724
725
726
727
728
const isExecutable = co.wrap(function *(repo, filename) {
    assert.instanceOf(repo, NodeGit.Repository);
    assert.isString(filename);
    const fullPath = path.join(repo.workdir(), filename);
    try {
        yield fs.access(fullPath, fs.constants.X_OK);
        return true;
    } catch (e) {
        // cannot execute
        return false;
fork icon51
star icon205
watch icon22

How does fs-promise.access work?

fs-promise.access is a promise-based version of Node.js' fs.access method, used to check if a file or directory exists and can be accessed with the specified permissions. It returns a Promise that resolves if the file or directory exists and the specified permissions are granted, and rejects with an error if the file or directory does not exist or the permissions are not granted.

Ai Example

1
2
3
4
5
6
const fsp = require("fs-promise");

fsp
  .access("/path/to/file")
  .then(() => console.log("File exists"))
  .catch(() => console.error("File does not exist"));

In this example, fs-promise.access is used to check if a file exists at the given path. The method returns a Promise that resolves if the file exists, and rejects if it doesn't.