VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-fspromises-opendir-method-2/

⇱ Node.js fsPromises.opendir() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js fsPromises.opendir() Method

Last Updated : 5 Aug, 2022

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:  

  • path: It is an String, Buffer or Url that specifies the path to the directory that has to be open.
  • options: It is an optional parameter that affects the output in someway accordingly provide it to the function call or not. 
    • encoding: It specifies the encoding technique, default is ‘UTF8’
    • bufferSize: It is a number that specifies the number of directory entries that are buffered internally when reading from the directory. High value of bufferSize, ensures good performance but leads to more memory usages.


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:  

  • dir.close() method: It asynchronously close the resources of the directory and hence Subsequent attempt to read will result in errors. A Promise is returned that will be resolved after the resource has been closed.
  • dir.closeSync() method: It synchronously close the resources of the directory and hence Subsequent attempt to read will result in errors.
  • dir.path: Returns the path to the directory.
  • dir.read() method: It asynchronously read the next directory entry. After the read is completed, a Promise is returned that will be resolved with an fs.Dirent, or null if there are no more directory read.


Example 1: 

Implementing the same functionality using async-await 

Output: 

 Directory is opened
 Path to the directory: test1
 Directory closed

 Further attempt to read sub-directories

 Error, Something went wrong!


Example 2: 

Implementing the same functionality using async-await 

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

Comment

Explore