Delete several files in node.js

后端 未结 8 1444
自闭症患者
自闭症患者 2021-02-15 23:58

What is the best way to delete several files in node.js?

function deleteFiles(files, callback){
    ...
}

var files = [\'file1.js\', \'file2.jpg\', \'file3.css\         


        
8条回答
  •  终归单人心
    2021-02-16 00:12

    I personally like shortcode (in one line)

    files.forEach(path => fs.existsSync(path) && fs.unlinkSync(path))
    

    So maybe using async/await style it's easier (complete example):

    try {
      var files = ['file1.js', 'file2.jpg', 'file3.css'];
      files.forEach(path => fs.existsSync(path) && fs.unlinkSync(path))
      // success code here
    } catch (err) {
      // error handling here
      console.error(err)
    }
    

提交回复
热议问题