Node.js fs.Dir.close() Method

The fs.Dir.close() method is an inbuilt application programming interface of class fs.Dir with in File System module which is used to close the directory’s underlying resource handle asynchronously.

Syntax:

const dir.close()

Parameter: This method does not accept any parameter.

Return Value: This method returns the promise which is just an enhancement to callback functions stop().

Below programs illustrates the use of fs.Dir.close() method in Node.js



Example 1:
Filename: GFG.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the 
// dir.close() method
const fs = require('fs');
  
// Initiating asyn function
async function stop(path) {
  
  // Creating and initiating directory's
  // underlying resource handle
  const dir = await fs.promises.opendir(path);
    
  // Asynchronously closing the directory's
  // underlying resource handle
  const promise = dir.close();
  
  // Display the result
  console.log(promise);
}
  
// Catching error
stop('./').catch(console.error);

chevron_right


Run GFG.js file using the following command:

node GFG.js

Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the 
// dir.close() API
const fs = require('fs');
  
// Intiating asyn function
async function stop(path) {
  
  let dir = null;
  
  try {
  
  // Creating and initiating directory's 
  // underlying resource handle
  dir = await fs.promises.
    opendir(new URL('file:///F:/java/'));
  
  } finally {
  
    if (dir) {
  
      // Display the result
      console.log("dir is closed successfully");
   
      // Close the file if it is opened.
      await dir.close();
    }
  }
}
  
// Catching error
stop('./').catch(console.error);

chevron_right


Run GFG.js file using the following command:

node GFG.js

Output:

Note: The above program will not run on online JavaScript and script editor.

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.