How to read file with async/await properly?

前端 未结 7 2139
时光说笑
时光说笑 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:13

    This is TypeScript version of @Joel's answer. It is usable after Node 11.0:

    import { promises as fs } from 'fs';
    
    async function loadMonoCounter() {
        const data = await fs.readFile('monolitic.txt', 'binary');
        return Buffer.from(data);
    }
    

提交回复
热议问题