转自:http://www.javashuo.com/article/p-rqhtpwqh-gt.htmlpromise
一、题目一async
async function async1(){ console.log('async1 start') await async2() console.log('async1 end') } async function async2(){ console.log('async2') } console.log('script start') setTimeout(function(){ console.log('setTimeout') },0) async1(); new Promise(function(resolve){ console.log('promise1') resolve(); }).then(function(){ console.log('promise2') }) console.log('script end')
二、题目二:post
async function async1(){ console.log('async1 start') await async2() console.log('async1 end') } function async2(){ // 去掉了 async 关键字
console.log('async2'); } console.log('script start') setTimeout(function(){ console.log('setTimeout') },0) async1(); new Promise(function(resolve){ console.log('promise1') resolve(); }).then(function(){ console.log('promise2') }) console.log('script end')
须要说明的是:spa
正常状况下,await
命令后面是一个 Promise 对象,返回该对象的结果。若是不是 Promise 对象,就直接返回对应的值(至关于直接Promise.resolve)。code
例子:对象
async function f() { // 等同于
// return 123;
return await 123; } f().then(v => console.log(v)) // 123
上面代码中,await
命令的参数是数值123
,这时等同于return 123
。blog