How to read file with async/await properly?

前端 未结 7 2136
时光说笑
时光说笑 2020-11-29 17:12

I cannot figure out how async/await works. I slightly understands it but I can\'t make it work.



        
相关标签:
7条回答
  • 2020-11-29 18:14

    You can use fs.promises available natively since Node v11.0.0

    import fs from 'fs';
    
    const readFile = async filePath => {
      try {
        const data = await fs.promises.readFile(filePath, 'utf8')
        return data
      }
      catch(err) {
        console.log(err)
      }
    }
    
    0 讨论(0)
提交回复
热议问题