fsPromises.opendir()方法在Node.js的文件系统模块中定义。文件系统模块本质上是用于与用户计算机的硬盘进行交互的。该方法用于异步打开目录。
fsPromise.opendir()方法返回已解决或被拒绝的诺言,因此避免了fs.opendir()中可能发生的回调嵌套或回调地狱问题。承诺通过“ fs.Dir”对象解析,该对象本身包含其他用于访问和关闭目录的函数。如果诺言被拒绝,它将被错误对象拒绝。
用法:
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:
// 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 attemp 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!')
})
使用async-await实现相同的函数
// 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 attemp 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:
// 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实现相同的函数
// 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
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js GM negative()用法及代码示例
- Node.js GM contrast()用法及代码示例
- Node.js GM whitePoint()用法及代码示例
- Node.js GM median()用法及代码示例
- Node.js GM gaussian()用法及代码示例
- Node.js GM crop()用法及代码示例
- Node.js GM despeckle()用法及代码示例
- Node.js GM whiteThreshold()用法及代码示例
- Node.js GM write()用法及代码示例
- Node.js GM gamma()用法及代码示例
- Node.js GM roll()用法及代码示例
- Node.js GM segment()用法及代码示例
- Node.js GM quality()用法及代码示例
- Node.js GM raise()用法及代码示例
注:本文由纯净天空筛选整理自hunter__js大神的英文原创作品 Node.js | fsPromises.opendir() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。