Node.js fsPromises.opendir() 方法
fsPromises.opendir()方法在 Node.js 的文件系统模块中定义。文件系统模块基本上是与用户计算机的硬盘进行交互的。该方法用于异步打开目录。
fsPromise.opendir()方法返回一个已解决或被拒绝的承诺,因此避免了 fs.opendir() 中可能发生的回调嵌套或回调地狱问题。承诺通过“fs.Dir”对象解决,该对象本身包含用于访问和关闭目录的其他功能。如果 promise 被拒绝,它将被错误对象拒绝。
句法:
fs.promises.opendir(path, options)
参数:此方法接受上面提到的两个参数,如下所述:
- path:它是一个 String、Buffer 或 Url,用于指定必须打开的目录的路径。
- options:它是一个可选参数,以某种方式影响输出,因此是否将其提供给函数调用。
- encoding:指定编码技术,默认为 'UTF8'
- bufferSize:它是一个数字,指定从目录读取时在内部缓冲的目录条目数。 bufferSize 的高值,可确保良好的性能,但会导致更多的内存使用。
返回值:此方法返回一个用 'fs.Dir' 对象解析的承诺,该对象本身包含用于访问和关闭目录的其他函数。如果 promise 被拒绝,它将被错误对象拒绝。
'dir' 对象方法:
- dir.close() 方法:它异步关闭目录的资源,因此后续尝试读取将导致错误。返回一个 Promise,将在资源关闭后解决。
- dir.closeSync() 方法:它同步关闭目录的资源,因此后续尝试读取将导致错误。
- dir.path:返回目录的路径。
- dir.read() 方法:它异步读取下一个目录条目。读取完成后,返回一个 Promise,将使用 fs.Dirent 解析,如果没有更多目录读取,则返回 null。
示例 1:
javascript
// Node.js program to demonstrate the
// fsPromises.opendir() Method
// Importing File System module
const fs = require('fs')
fs.promises.opendir('./test1')
.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!')
})
javascript
// Node.js program to demonstrate the
// fsPromises.opendir() Method
// Importing File System module
const fs = require('fs')
const readDir = async (path) => {
// Opeaning directory
const dir = await fs.promises.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)
}
readDir('./test1')
.catch(err => {
console.log('Error, Something went wrong!')
})
javascript
// Node.js program to demonstrate the
// fsPromises.opendir() Method
// Importing File System module
const fs = require('fs')
fs.promises.opendir('./test1')
.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!')
})
javascript
// Node.js program to demonstrate the
// fsPromises.opendir() Method
// Importing File System module
const fs = require('fs')
const readDir = async (path) => {
// Opeaning directory
const dir = await fs.promises.opendir(path)
console.log('Directory is opened')
// Path to the directory
console.log(`Path to the directory: ${dir.path}`)
// Reading sub-directories or files
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}`)
}
readDir('./test1')
.catch(err => {
console.log('Error, Something went wrong!')
})
使用 async-await 实现相同的功能
javascript
// Node.js program to demonstrate the
// fsPromises.opendir() Method
// Importing File System module
const fs = require('fs')
const readDir = async (path) => {
// Opeaning directory
const dir = await fs.promises.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)
}
readDir('./test1')
.catch(err => {
console.log('Error, Something went wrong!')
})
输出:
Directory is opened
Path to the directory: test1
Directory closed
Further attempt to read sub-directories
Error, Something went wrong!
示例 2:
javascript
// Node.js program to demonstrate the
// fsPromises.opendir() Method
// Importing File System module
const fs = require('fs')
fs.promises.opendir('./test1')
.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!')
})
使用 async-await 实现相同的功能
javascript
// Node.js program to demonstrate the
// fsPromises.opendir() Method
// Importing File System module
const fs = require('fs')
const readDir = async (path) => {
// Opeaning directory
const dir = await fs.promises.opendir(path)
console.log('Directory is opened')
// Path to the directory
console.log(`Path to the directory: ${dir.path}`)
// Reading sub-directories or files
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}`)
}
readDir('./test1')
.catch(err => {
console.log('Error, Something went wrong!')
})
输出:
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
参考: https://nodejs.org/dist/latest-v14.x/docs/api/fs.html#fs_fspromises_opendir_path_options