Async function 在 2017 年已到达了 stage 4。
async/await 使用注意点
为了不让程序挂掉, 注意捕获错误
写法 1: 用try/catch捕获async function test1() {try {await Promise.reject(new Error('boom'))} catch(e) {console.log(e)}console.log('go on')}test1()// Error: boom// go on
写法 2: 直接在
await后面的promise上进行catchasync function test2() {await Promise.reject(new Error('boom')).catch(e => console.log(e))console.log('go on')}test2()// Error: boom// go on
使用并行提高程序的运行速度
// 串行调用示范async function block() {const result1 = await request(url1)const result2 = await request(url2)}
在如上案例中,
request(url1)请求未完成的话是不会发起request(url2)请求的(类似串行调用), 若想使之变为并行调用可以作如下修改:
写法1:async function block() {const promise1 = request(url1)const promise2 = request(url2)const result1 = await promise1const result2 = await promise2}
写法2:
async function block() {const [result1, result2] = await Promise.all([request(url1), request(url2)])}
async 函数的实现原理
async function fn {}
async函数在低版本浏览器中其实就是转为co + Generatorfunction fn {return co(function* () {})}
关于简版
co, 可以看在 Generator 函数的异步应用 的实现。异步遍历器
proposal-async-iteration, 异步遍历器也已经到了 stage 4。
异步迭代器
同步场景下可以通过如下获取
value、done:const { value, done } = syncIterator.next()
异步场景下可以通过如下方式获取
value、doneasyncIterator.next().then({ value, done } => {})
for await .. of
for await (const line of readLines(filePath)) {console.log(line)}
