How to operate callback-based fs.lstat() method with promises in Node.js ? Last Updated : 12 Jul, 2021 Comments Improve Suggest changes Like Article Like Report The fs.lstat() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user’s computer. The lstat() method gives some information specific to files and folders using methods define on stats objects (data provided by lstat).The fs.lstat() method is based on callback. Using callback methods lead to a great chance of callback nesting or callback hell problems. Thus to avoid it, we almost always like to work with a promise-based method. Using some extra node.js methods we can operate a callback-based method in promise way.Syntax: fs.lstat(path, options) Note: Callback not required since we operate the method with promises.Parameter: This method accept two parameters as mentioned above and described below: path: It is an string, buffer or url that specifies the path to the directory, whose contents we try to read.options: It is an optional parameter. One options parameter is ‘bigint’, it is a boolean value. Here we specify if the numeric value in returned stats object by fs.lstat() is bigint or not(default-false). Return Value: If method operates with promises, it returns a resolved or rejected promise. Promise is resolved with stats object if the directory is successfully read otherwise rejected with an error object if any error occurs(example-specified directory not exist or does not have permissions to read files, etc).The stats object returned from the resolved promise has some properties and methods defined in it which helps in getting some specific details about the target files or folders. Some of the methods are specifies below. stats.isDirectory(): It returns true if stats object describes a file system directory.stats.isFile(): It returns true if stats object describes a regular file.stats.isSocket(): It returns true if stats object describes a socket.stats.isSymbolicLink(): It returns true if stats object describes a symbolic link.stats.isFile(): It returns true if stats object describes a regular file.stats.isFIFO(): It returns true if stats object describes first in first out pipe.stats.size: It specifies the size of the file in bytes.stats.blocks: It specifies the number of blocks allocated for the file. Example 1: Filename: index.js javascript // Program to identify files and folders // of a directory // Importing File System and Utilities module const fs = require('fs') const util = require('util') // Convert callback based methods to // promise based methods const readDir = util.promisify(fs.readdir) const lStat = util.promisify(fs.lstat) const fileOrFolder = async (path) => { const filenames = await readDir(path) for (let filename of filenames) { // Calling lstat method to give the // stats object for every directory const stats = await lStat(filename) // Check file or folder if (stats.isFile()) { console.log( `${filename} ---------> File`) } else { console.log( `${filename} ---------> Folder`) } } } // Driver code // The process.cwd() gives current // working directory fileOrFolder(process.cwd()) // If promise is rejected .catch(err => { console.log(`Error occurs, Error code -> ${err.code}, Error No -> ${err.errno} `); }); Run index.js file using the following command: node index.js Output: Example 2: Filename: index.js javascript // Program to count the size of // directories in bytes // Importing File System and // Utilities module const fs = require('fs') const util = require('util') // Convert callback based methods to // promise based methods const readDir = util.promisify(fs.readdir) const lStat = util.promisify(fs.lstat) const sizeOfDirectories = async (path) => { const filenames = await readDir(path) for (let filename of filenames) { // Calling lstat method to give the // stats object for every directory const stats = await lStat(filename) // Print size of each directory console.log(`${filename} --------> ${stats.size} bytes`) } } // Driver code // The process.cwd() gives current // working directory sizeOfDirectories(process.cwd()) // If promise is rejected .catch(err => { console.log(`Error occurs, Error code -> ${err.code}, Error No -> ${err.errno}`); }) Run index.js file using the following command: node index.js Output: Comment More infoAdvertise with us Next Article How to operate callback-based fs.lstat() method with promises in Node.js ? hunter__js Follow Improve Article Tags : Web Technologies Node.js Node.js-Misc Node.js-fs-module Similar Reads How to operate callback-based fs.mkdir() method with promises in Node.js ? The fs.mkdir() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user's computer. The mkdir() method is used to asynchronously create a directory. The fs.mkdir() method is based on callback. Using callback methods leads 4 min read How to operate callback-based fs.rename() method with promises in Node.js ? The fs.rename() method is defined in the File System module of Node.js. The File System module is basically to interact with hard-disk of the userâs computer. The rename() method is used to rename the file at the given old path to a given new path. If the new path file already exists, it will be ove 5 min read How to operate callback-based fs.readdir() method with promises in Node.js ? The fs.readdir() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the userâs computer. The readdir() method is used to read the contents of a directory. The fs.readdir() method is based on callback. Using callback methods l 3 min read How to operate callback-based fs.readFile() method with promises in Node.js ? The fs.readFile() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the userâs computer. The readFile() method is used to asynchronously read the entire contents of a file and returns the buffer form of the data. The fs.read 5 min read How to operate callback-based fs.opendir() method with promises in Node.js ? The fs.opendir() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the userâs computer. The method used to asynchronously open a directory.The fs.opendir() method is based on callback. Using callback methods leads to a great 5 min read How to operate callback-based fs.truncate() method with promises in Node.js ? The fs.truncate() method defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the userâs computer. The truncate() method is used to modify the inner contents of the file by âlenâ bytes. If len is shorter than the fileâs current length, t 4 min read How to operate callback based fs.writeFile() method with promises in Node.js ? The fs.writeFile() is a method defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the userâs computer. The fs.writeFile() method asynchronously writes data to a file, replacing the file if it already exists. The fs.writeFile() method i 5 min read How to operate callback based fs.appendFile() method with promises in Node.js ? The fs.appendFile() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the userâs computer. The appendFile() method is used to append new data in the existing file or if the file does not exist then the file is created first 4 min read How to convert function call with two callbacks promise in Node.js ? Promise: Promise is used to handle the result, if it gets the required output then it executes the code of then block, and if it gets the error then it executes the code of the catch block.A promise looks like this -function() .then(data => { // After promise is fulfilled console.log(data); }) .c 2 min read How to Convert an Existing Callback to a Promise in Node.js ? Node.js, by nature, uses an asynchronous, non-blocking I/O model, which often involves the use of callbacks. While callbacks are effective, they can lead to complex and hard-to-read code, especially in cases of nested callbacks, commonly known as "callback hell." Promises provide a cleaner, more rea 7 min read Like