How to use the promises function from graceful-fs

Find comprehensive JavaScript graceful-fs.promises code examples handpicked from public code repositorys.

graceful-fs.promises is a module that provides a Promise-based API for interacting with the file system in Node.js, using the fs module with graceful fallbacks for certain errors.

1
2
3
4
5
6
7
8
9
10
11
12
'use strict';


const Promise = require('bluebird');
const fs = require('graceful-fs');
const { dirname, join, extname, basename } = require('path');
const fsPromises = fs.promises;
const chokidar = require('chokidar');
const { escapeRegExp } = require('hexo-util');


const rEOL = /\r\n/g;
fork icon9
star icon48
watch icon0

+ 7 other calls in file

896
897
898
899
900
901
902
903
904
async getInputFileStat() {
  if (this._stats) {
    return this._stats;
  }

  this._stats = fs.promises.stat(this.inputPath);

  return this._stats;
}
fork icon432
star icon0
watch icon89

+ 8 other calls in file

How does graceful-fs.promises work?

graceful-fs.promises is an extension of the Node.js fs.promises API that makes file system operations more robust by providing better error handling and supporting graceful degradation of operations under certain error conditions. It is built on top of the graceful-fs library, which provides a more reliable file system API by addressing a number of issues and edge cases in the Node.js fs module.

1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
const certificateDir = Server.findCacheDir();
const certificatePath = path.join(certificateDir, "server.pem");
let certificateExists;

try {
  const certificate = await fs.promises.stat(certificatePath);
  certificateExists = certificate.isFile();
} catch {
  certificateExists = false;
}
fork icon0
star icon0
watch icon1

+ 35 other calls in file

100
101
102
103
104
105
106
107
108
109
async function go () {
  log.verbose('ensuring nodedir is created', devDir)

  // first create the dir for the node dev files
  try {
    const created = await fs.promises.mkdir(devDir, { recursive: true })

    if (created) {
      log.verbose('created nodedir', created)
    }
fork icon0
star icon0
watch icon1

+ 197 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const fsp = require("graceful-fs/promises");

async function readMyFile() {
  try {
    const data = await fsp.readFile("/path/to/my/file.txt", "utf8");
    console.log(data);
  } catch (err) {
    console.error(err);
  }
}

readMyFile();

In this example, we import the graceful-fs/promises module and use its readFile() method to read the contents of a file located at /path/to/my/file.txt. We use the async/await syntax to handle the asynchronous operation, and we catch any errors that may occur using a try-catch block. When the file contents have been successfully read, we log them to the console.

2295
2296
2297
2298
2299
2300
2301
2302
2303
2304

if (this.options.ipc) {
  // chmod 666 (rw rw rw)
  const READ_WRITE = 438;

  await fs.promises.chmod(this.options.ipc, READ_WRITE);
}

if (this.options.webSocketServer) {
  this.createWebSocketServer();
fork icon0
star icon0
watch icon1

+ 41 other calls in file

15
16
17
18
19
20
21
22
23
24
class FSAsync {

        get access()     { return gracefulFs.promises.access; }
        get appendFile() { return gracefulFs.promises.appendFile; }
        get chmod()      { return gracefulFs.promises.chmod; }
        get chown()      { return gracefulFs.promises.chown; }
        get copyFile()   { return gracefulFs.promises.copyFile; }
        get lchmod()     { return gracefulFs.promises.lchmod; }
        get lchown()     { return gracefulFs.promises.lchown; }
        get link()       { return gracefulFs.promises.link; }
fork icon0
star icon0
watch icon2

+ 95 other calls in file