Node.js fs.Dir.close() 方法
fs.Dir.close()方法是文件系统模块中fs.Dir类的内置应用程序编程接口,用于异步关闭目录的底层资源句柄。
句法:
const dir.close()
参数:此方法不接受任何参数。
返回值:此方法返回的承诺只是对回调函数 stop() 的增强。
下面的程序说明了 Node.js 中fs.Dir.close()方法的使用
示例 1:
文件名:GFG.js
Javascript
// Node program to demonstrate the
// dir.close() method
const fs = require('fs');
// Initiating async 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);
Javascript
// Node program to demonstrate the
// dir.close() API
const fs = require('fs');
// Initiating async 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);
使用以下命令运行GFG.js文件:
node GFG.js
输出:
示例 2:
Javascript
// Node program to demonstrate the
// dir.close() API
const fs = require('fs');
// Initiating async 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);
使用以下命令运行GFG.js文件:
node GFG.js
输出:
注意:上述程序不会在在线 JavaScript 和脚本编辑器上运行。
参考: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_close