如何在 Node.js 中使用 Promise 操作基于回调的 fs.readdir() 方法?
fs.readdir()方法在 Node.js 的文件系统模块中定义。文件系统模块基本上是与用户计算机的硬盘进行交互的。 readdir() 方法用于读取目录的内容。
fs.readdir() 方法基于回调。使用回调方法导致回调嵌套或回调地狱问题的可能性很大。因此,为了避免这种情况,我们几乎总是喜欢使用基于 Promise 的方法。使用一些额外的 node.js 方法,我们可以以 promise 的方式操作基于回调的方法。
句法:
fs.readdir(path, options)
注意:不需要回调,因为我们使用 Promise 操作该方法。
参数:此方法接受上面提到的两个参数,如下所述:
- path:它是一个字符串、缓冲区或 url,它指定目录的路径,我们尝试读取其内容。
- options:它是一个可选参数,这里我们指定编码技术(default-utf8)等。
方法:基于回调的 fs.readdir() 方法。要使用 Promise 操作它,首先,我们使用实用程序模块中定义的 promisify() 方法将其转换为基于 Promise 的方法。
示例 1:文件名:index.js
// Program to read file and folders of
// the current working directory
// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
// Convert callback based methods to promise
// based methods
const readDir = util.promisify(fs.readdir)
// Reading current working directory
readDir(process.cwd())
// If promise resolved and datas are fetched
.then(filenames => {
for(let filename of filenames) {
console.log(filename)
}
})
// If promise is rejected
.catch(err => {
console.log(`Error occurs,
Error code -> ${err.code},
Error No -> ${err.errno} `);
})
使用 async-await 实现相同的功能:
// Program to read file and folders of the
// current working directory
// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
// Convert callback based methods to promise
// based methods
const readDir = util.promisify(fs.readdir)
const readDirectory = async (path) => {
const filenames = await readDir(path)
for(let filename of filenames){
console.log(filename)
}
}
readDirectory(process.cwd())
// If promise is rejected
.catch(err => {
console.log(`Error occurs,
Error code -> ${err.code},
Error No -> ${err.errno}`);
})
使用以下命令运行 index.js 文件:
node index.js
输出:
示例 2:文件名:index.js
// Program to read file and folders of the
// current working directory or as the path
// given by command line argument
// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
// Convert callback based methods to promise
// based methods
const readDir = util.promisify(fs.readdir)
// The process.cwd() gives current working directory
const targetDir = process.argv[2] || process.cwd()
readDir(targetDir)
// If promise resolved and datas are fetched
.then(filenames => {
for(let filename of filenames) {
console.log(filename)
}
})
// If promise is rejected
.catch(err => {
console.log(err)
})
使用 async-await 实现相同的功能:
// Program to read file and folders of the
// current working directory or as the path
// given by command line argument
// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
// The process.cwd() gives current working directory
const targetDir = process.argv[2] || process.cwd()
// Convert callback based methods to promise
// based methods
const readDir = util.promisify(fs.readdir)
const readDirectory = async (path) => {
const filenames = await readDir(path)
for(let filename of filenames){
console.log(filename)
}
}
readDirectory(targetDir)
// If promise is rejected
.catch(err => {
console.log(`Error occurs,
Error code -> ${err.code},
Error No -> ${err.errno}`);
})
使用以下命令运行 index.js 文件:
node index.js
输出: