导语:异步编程的最高境界就是不关心它是否是异步。async、await很好的解决了这一点,将异步强行转换为同步处理。
async/await与promise不存在谁代替谁的说法,因为async/await是寄生于Promise,是Generater的语法糖。
温馨提示:如果你已经知道了关于async await的基本用法,请直接看分割线以下内容
Generator
function* helloWorld () { yield ‘hello‘ yield ‘world‘ return ‘ending‘ } var hw = helloWorld() console.log(hw) // helloWorld {<suspended>} console.log(hw.next()) // {value: "hello", done: false} console.log(hw.next()) // {value: "world", done: false} console.log(hw.next()) // {value: "world", done: false} console.log(hw.next()) // {value: undefined, done: true}
async function fn () { await 100 await 200 return 300 } fn().then(res => { console.log(res) // 300 })
打印结果如下:(返回的是promise对象)
如果在async函数中抛出了错误,则终止错误结果,不会继续向下执行:
async function f() { try { await Promise.reject(‘出错了‘); } catch(e) { } return await Promise.resolve(‘hello world‘); } f() .then(v => console.log(v)) // hello world // catch async function f() { await Promise.reject(‘出错了‘) .catch(e => console.log(e)); // 出错了 return await Promise.resolve(‘hello world‘); } f() .then(v => console.log(v)) // hello world
================================ 分割线 ==================================
面试题
【例1】
async function async1() { console.log("async1 start"); await async2(); console.log("async1 end"); return ‘async return‘; } async function async2() { console.log("async2"); } console.log("script start"); setTimeout(function() { console.log("setTimeout"); }, 0); async1().then(function (message) { console.log(message) }); new Promise(function(resolve) { console.log("promise1"); resolve(); }).then(function() { console.log("promise2"); }); console.log("script end");
输出顺序如下:
[分析]:
1. 首先我们从上到下看,打印:script start;
2. setTimeout 虽然是0,但依然要放到执行栈,等待后续执行。继续往下走;
3. async1 开始执行,当函数里遇到await时,暂停执行(await所在行放在本次执行完),执行完后遇到then是异步,此时async1被添加到任务队列;
4. new Promise() 实例对象被new出来后,它里面的promise1会立刻打印,然后又遇到then,此时promise对象被添加到任务队列;
【例2】:
var p = new Promise((res,rej) => { res(‘hello one‘) console.log(‘good morning‘) }) function hello() { console.log(‘hello begins‘) return p } hello().then(res => { console.log(res) console.log(‘hello1111111111‘) return ‘hello two‘ }).then(res => { console.log(res) console.log(‘hello22222222222‘) return ‘hello three‘ }).then(res => { console.log(res) console.log(‘hello33333333333‘) }) function test1 () { console.log(‘test1‘) } async function asy () { console.log(‘asy begins‘) await console.log(‘asy---111111‘) console.log(‘async1‘) await console.log(‘asy---222222‘) console.log(‘asy ends‘) } asy() test1() function* gnrt () { console.log(1) yield console.log(11111111) console.log(2) yield console.log(22222222) console.log(3) yield console.log(33333333) } var result = gnrt() result.next() result.next() result.next()
输出顺序如下:
promise async await 基本用法和面试题详解
原文:https://www.cnblogs.com/edwardwzw/p/12033935.html