📌  相关文章
📜  如何在 Node.js 中使用 Promise 操作基于回调的 fs.mkdir() 方法?

📅  最后修改于: 2022-05-13 01:56:21.500000             🧑  作者: Mango

如何在 Node.js 中使用 Promise 操作基于回调的 fs.mkdir() 方法?

fs.mkdir()方法在 Node.js 的文件系统模块中定义。文件系统模块基本上是与用户计算机的硬盘进行交互的。 mkdir() 方法用于异步创建目录。

fs.mkdir() 方法基于回调。使用回调方法导致回调嵌套或回调地狱问题的可能性很大。因此,为了避免这种情况,我们几乎总是喜欢使用基于 Promise 的方法。使用一些额外的 node.js 方法,我们可以以 promise 的方式操作基于回调的方法。

句法:

fs.mkdir(path, options)

注意:不需要回调,因为我们使用 Promise 操作该方法。

参数:此方法接受上面提到的两个参数,如下所述:

  • 路径:它是一个字符串、缓冲区或 URL,指定必须创建的目录的路径。
  • options:它是一个可选参数,它以某种方式影响输出,因此我们是否将其提供给函数调用。
    • mode:字符串或整数,用于设置目录权限。默认情况下,它的值为 0.777
    • recursive:它是一个布尔值,它指定是否应该创建父目录。默认情况下,它的值为 false。

方法:基于回调的 fs.mkdir() 方法。要使用 Promise 操作它,首先,我们使用实用程序模块中定义的 promisify() 方法将其转换为基于 Promise 的方法。

示例 1:文件名:index.js

// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
  
// Convert callback based methods to promise
// based methods
const makeDir = util.promisify(fs.mkdir)
const readDir = util.promisify(fs.readdir)
  
// Create new directory
makeDir(dir='./Test Directory')
.then(() => {
  console.log(`Directory '${dir}' is created`)
})
  
// If promise gets rejected
.catch(err => {
    console.log(`Error occurs, 
    Error code -> ${err.code},
    Error No -> ${err.errno}`);
})

使用 async await 实现相同的功能:

// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
  
// Convert callback based methods to promise
// based methods
const makeDir = util.promisify(fs.mkdir)
const readDir = util.promisify(fs.readdir)
  
const createDirectory = async path => {
  await makeDir(path)
  console.log(`Directory '${path}' is created`)
}
  
createDirectory('./TestDirectory')
  
// If promise gets rejected
.catch(err => {
    console.log(`Error occurs, 
    Error code -> ${err.code},
    Error No -> ${err.errno}`);
})

使用以下命令运行 index.js 文件:

node index.js

运行程序前的文件内容:

运行程序后的文件内容:

输出:

Directory './Test Directory' is created

示例 2:文件名:index.js

// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
  
// Convert callback based methods to 
// promise based methods
const makeDir = util.promisify(fs.mkdir)
const readDir = util.promisify(fs.readdir)
  
console.log(`\nBefore creating new Directory : \n`)
readDir(process.cwd())
.then(filenames => {
  
   // Fetch the contents of current working
   // directory before creating new directory
   for(let filename of filenames){
      console.log(filename)
   }
})
.catch(err => {
   console.log(`Error occurs, 
   Error code -> ${err.code},
   Error No -> ${err.errno}`);
})
  
// Create new directory
makeDir('./Test Directory')
.then(() => {
  
   // Fetch the contents of current working
   // directory after creating new directory
   console.log(`\nAfter creating new directory : \n`)
     
   return readDir(process.cwd())
})
  
.then(filenames => {
  for(let filename of filenames) {
    console.log(filename)
  }
})
  
// If promise gets rejected
.catch(err => {
  console.log(`Error occurs, 
  Error code -> ${err.code},
  Error No -> ${err.errno}`)
})

使用 async await 实现相同的功能:

// Importing File System and Utilities module
const fs = require('fs')
const util = require('util')
  
// Convert callback based methods to promise
// based methods
const makeDir = util.promisify(fs.mkdir)
const readDir = util.promisify(fs.readdir)
  
const createDirectory = async path => {
  console.log(`\nBefore creating new Directory : \n`)
    
  // Fetch the contents of current working directory
  // before creating new directory
  const oldContents = await readDir(process.cwd())  
  for(let filename of oldContents) {
    console.log(filename)
  }
  
  // Create new directory
  await makeDir('./Test Directory')
  console.log(`\nAfter creating new directory : \n`)
  
  // Fetch the contents of current working directory
  // after creating new directory
  const newContents = await readDir(process.cwd())
  for(let filename of newContents) {
    console.log(filename)
  }
}
  
createDirectory('./TestDirectory')
// If promise gets rejected
.catch(err => {
   console.log(`Error occurs, 
   Error code -> ${err.code},
   Error No -> ${err.errno}`);
});

使用以下命令运行 index.js 文件:

node index.js

运行程序前的文件内容:

运行程序后的文件内容:

输出: