Open In App

Node.js fsPromises.opendir() Method

The fsPromises.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 is used to asynchronously open a directory.
The fsPromise.opendir() method returns a resolved or rejected promise and hence avoid the callback nesting or callback hell problems that may occur in fs.opendir(). The promise is resolved with ‘fs.Dir’ object, the object itself contains other functions for accessing and closing the directory. In case if the promise is rejected it will be rejected with an error object.
Syntax:  

fs.promises.opendir(path, options)

Parameters: This method accept two parameters as mentioned above and described below:  



Return Value: This method returns a promise resolved with ‘fs.Dir’ object, the object itself contains other functions for accessing and closing the directory. In case if the promise is rejected, it will be rejected with an error object.
‘dir’ object methods:  

Example 1: 






// Node.js program to demonstrate the  
// fsPromises.opendir() Method
 
// Importing File System module
const fs = require('fs')
 
fs.promises.opendir('./test1')
    .then(dir => {
        console.log('Directory is opened')
 
        // Path to the directory
        console.log(
        `Path to the directory: ${dir.path}`)
 
        // Closing directory
        return dir.close()
    })
 
    .then(() => {
        console.log('Directory closed')
 
        console.log('\nFurther attempt to'
            + ' read sub-directories\n')
 
        // Further attempt to access the
        // directory results in error
        return dir.read()
    })
 
    .then(dirent => {
 
        // Does not execute since directory
        // is closed catch block runs instead
        console.log(dirent)
    })
    .catch(err => {
        console.log('Error, Something went wrong!')
    })

Implementing the same functionality using async-await 




// Node.js program to demonstrate the  
// fsPromises.opendir() Method
 
// Importing File System module
const fs = require('fs')
 
const readDir = async (path) => {
 
    // Opening directory
    const dir = await fs.promises.opendir(path)
    console.log('Directory is opened')
 
    // Path to the directory
    console.log(`Path to the directory: ${dir.path}`)
 
    // Closing directory
    await dir.close()
    console.log('Directory closed')
 
    console.log('\nFurther attempt '
            + 'to read sub-directories\n')
 
    // Further attempt to access the directory
    // results in error does not execute
    // since directory is closed catch
    // block runs instead
    const subDir = await dir.read()
    console.log(subDir)
}
 
readDir('./test1')
    .catch(err => {
        console.log('Error, Something went wrong!')
    })

Output: 

  Directory is opened
  Path to the directory: test1
  Directory closed

  Further attempt to read sub-directories

  Error, Something went wrong!

Example 2: 




// Node.js program to demonstrate the  
// fsPromises.opendir() Method
 
// Importing File System module
const fs = require('fs')
 
fs.promises.opendir('./test1')
    .then(dir => {
        console.log('Directory is opened')
 
        // Path to the directory
        console.log(`Path to the directory: ${dir.path}`)
 
        // Reading sub-directories or files
 
        console.log('\nReading sub-directories:\n')
        return dir.read()
    })
 
    .then(dirent => {
        console.log(`Sub-Directory : ${dirent.name}`)
 
        // Reading further sub directories
        return dir.read()
    })
 
    .then(dirent => {
        console.log(`Sub-Directory : ${dirent.name}`)
        return dir.read()
    })
 
    .then(dirent => {
        console.log(`Sub-Directory : ${dirent.name}`)
        return dir.read()
    })
    .then(dirent => {
        console.log(`Sub-Directory : ${dirent.name}`)
        return dir.read()
    })
    .then(dirent => {
        console.log(`Sub-Directory : ${dirent.name}`)
        return dir.read()
    })
    .catch(err => {
        console.log('Error, Something went wrong!')
    })

Implementing the same functionality using async-await 




// Node.js program to demonstrate the  
// fsPromises.opendir() Method
 
// Importing File System module
const fs = require('fs')
 
const readDir = async (path) => {
 
    // Opening directory
    const dir = await fs.promises.opendir(path)
    console.log('Directory is opened')
 
    // Path to the directory
    console.log(`Path to the directory: ${dir.path}`)
 
    // Reading sub-directories or files
    const subDir1 = await dir.read()
    const subDir2 = await dir.read()
    const subDir3 = await dir.read()
    const subDir4 = await dir.read()
    const subDir5 = await dir.read()
 
    // Printing
    console.log(`Sub-Directory : ${subDir1.name}`)
    console.log(`Sub-Directory : ${subDir2.name}`)
    console.log(`Sub-Directory : ${subDir3.name}`)
    console.log(`Sub-Directory : ${subDir4.name}`)
    console.log(`Sub-Directory : ${subDir5.name}`)
}
 
readDir('./test1')
    .catch(err => {
        console.log('Error, Something went wrong!')
    })

Output: 

  Directory is opened
  Path to the directory: test1

  Reading sub-directories:

  Sub-Directory : example1.txt
  Sub-Directory : example2.txt
  Sub-Directory : example3.txt
  Sub-Directory : example4.txt
  Sub-Directory : null

Reference: https://nodejs.org/dist/latest-v14.x/docs/api/fs.html#fs_fspromises_opendir_path_options


Article Tags :