async/await是ES8引入的新语法,用来简化Promise异步操作。在async/await出现之前,开发者只能通过链式.then()的方式处理Promise异步操作。
.then链式调用解决了回调地狱的问题,但是代码冗余、阅读性差、不易理解。
async/await的基本使用
@H_502_6@
import thenFs from then-fs async function getAllFile() { const r1 = await thenFs.readFile('./file/1.txt','utf8') console.log(r1)
const r2 = await thenFs.readFile('./file/2.txt','utf8')
console.log(r2)
const r3 = await thenFs.readFile('./file/3.txt','utf8')
console.log(r3)
}
getAllFile()
async和await使用注意事项:
@H_502_6@
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。