Node.js fs.Dir.close() Method
Last Updated :
02 Sep, 2021
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
Javascript
const fs = require( 'fs' );
async function stop(path) {
const dir = await fs.promises.opendir(path);
const promise = dir.close();
console.log(promise);
}
stop( './' ). catch (console.error);
|
Run GFG.js file using the following command:
node GFG.js
Output:
Example 2:
Javascript
const fs = require( 'fs' );
async function stop(path) {
let dir = null ;
try {
dir = await fs.promises.
opendir( new URL('file:
} finally {
if (dir) {
console.log( "dir is closed successfully" );
await dir.close();
}
}
}
stop('./'). catch (console.error);
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...