Open In App

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 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. 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.opendir(path, options)

Note : Callback not required since we operate the method with promises.
Parameters: Accepts two parameter path and options as mentioned above. The options is an optional parameter.  



Returns: If method operates with promises it 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: 

Approach: fs.opendir() method based on callback. To operate it with promises, first, we use promisify() method defined in the utilities module to convert it into a promise based method.
Example 1: Filename: index.js 






// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
 
// Convert callback based methods
// to promise based methods
const openDir = util.promisify(fs.opendir)
 
openDir('./testDirectory')
    .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. 




// Importing File System and
// Utilities module
const fs = require('fs')
const util = require('util')
 
// Convert callback based methods
// to promise based methods
const openDir = util.promisify(fs.opendir)
 
const fetchDirectory = async (path) => {
 
    // Opeaning directory
    const dir = await 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)
}
 
fetchDirectoey('./testDirectory')
    .catch(err => {
        console.log('Error, Something went wrong!')
    })

Run index.js file using the following command: 
 

node index.js

Output: 
 

  Directory is opened
  Path to the directory: testDirectory
  Directory closed

  Further attempt to read sub-directories:

  Error, Something went wrong!

Example 2: Filename index.js 
 




// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
 
// Convert callback based methods to
// promise based methods
const openDir = util.promisify(fs.opendir)
 
openDir('./testDirectory')
    .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. 
 




// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
 
// Convert callback based methods
// to promise based methods
const openDir = util.promisify(fs.opendir)
 
const fetchDirectory = async (path) => {
 
    // Opeaning directory
    const dir = await openDir(path)
    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')
 
    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}`)
}
 
fetchDirectory('./testDirectory')
    .catch(err => {
        console.log('Error, Something went wrong!')
    })

Run index.js file using the following command: 
 

node index.js

Output: 
 

  Directory is opened
  Path to the directory: testDirectory

  Reading sub-directories:

  Sub-Directory : testFile1.txt
  Sub-Directory : testFile2.txt
  Sub-Directory : teasrFile3.txt
  Sub-Directory : testFile4.txt
  Sub-Directory : null

 


Article Tags :