在nodejs中异步创建文件夹

Mik*_*kau 0 asynchronous node.js

我尝试制作简单的脚本来创建一个文件夹(如果它不存在)。我读了一些文章并做出了这样的逻辑:

debug("before async");
(async () => {
  if(!fs.existsSync(outputPath)){
    debug("Folder not exsists! path: "+outputPath)
    try{
      return await fs.mkdir(outputPath)
    }catch(err){
      debug(err)
    }
  }
  res.send('<h1>Hello world!</h1>')
})()
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

(node:27611) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
Run Code Online (Sandbox Code Playgroud)

好的。我想了一下,并提醒 stackoverflow 的那个人告诉我将回调函数设置为 promisify。我试着做到:

const mkdir = util.promisify(fs.mkdir);
debug("Before async");
(async () => {
  if(!fs.existsSync(outputPath)){
    debug("Folder not exsists! path: "+outputPath)
    await Promise.all(mkdir(outputPath))
  }
  res.send('<h1>Hello world!</h1>')
})()
Run Code Online (Sandbox Code Playgroud)

但我得到了其他错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?如果您知道任何可以帮助我了解异步功能的指南,那就太好了。谢谢!

顺便说一句,两种方式的文件夹都已创建。但有错误...

Дми*_*ьев 9

节点 11.xx 及更高版本

await fs.promises.mkdir( '/path/mydir' );
Run Code Online (Sandbox Code Playgroud)