promise 与 await

async function async1() {
 console.log("async1 start");
 await  async2(); 
 const newPromise6 = new Promise(function(resolve, reject) {
   resolve(console.log('promise11111'));
 }).then(() => {console.log('then2222222')});
 console.log("async1 end");
}

async  function async2() {
 setTimeout(()=>console.log(5555),0);
}

console.log("script start");

setTimeout(function () {
 console.log("settimeout");
},0);

async1();

new Promise(function (resolve) {
 resolve(console.log("promise1"));
}).then(function () {
 console.log("promise2");
});

console.log('script end');

执行结果

script start
async1 start
promise1
script end
promise11111
async1 end
promise2
then2222222
settimeout
5555

说明:async2为settimeout时,会将async1中await async2()下面的代码放入微任务队列中。

改变async2

async function async1() {
 console.log("async1 start");
 await  async2(); 
 const newPromise6 = new Promise(function(resolve, reject) {
   resolve(console.log('promise11111'));
 }).then(() => {console.log('then2222222')});
 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) {
 resolve(console.log("promise1"));
}).then(function () {
 console.log("promise2");
});

console.log('script end');

执行结果

script start
async1 start
async2
promise1
script end
promise11111
async1 end
promise2
then2222222
settimeout

说明:async2为console.log时,也依然会将async1中await async2()下面的代码放入微任务队列中。

async function async1() {
 console.log("async1 start");
 await  async2(); 
 const newPromise6 = new Promise(function(resolve, reject) {
   resolve(console.log('promise11111'));
 }).then(() => {console.log('then2222222')});
 console.log("async1 end");
}

async  function async2() {
 Promise.resolve('sssss').then(res=>console.log(res));
}

console.log("script start");

setTimeout(function () {
 console.log("settimeout");
},0);

async1();

new Promise(function (resolve) {
 resolve(console.log("promise1"));
}).then(function () {
 console.log("promise2");
});

console.log('script end');

执行结果

script start
async1 start
promise1
script end
sssss
promise11111
async1 end
promise2
then2222222
settimeout

说明:当async2不返回promise时,会先将async2的then放入微任务队列中,当该then执行结束时,立即执行async1剩下的代码
async function async1() {
 console.log("async1 start");
 await  async2(); 
 const newPromise6 = new Promise(function(resolve, reject) {
   resolve(console.log('promise11111'));
 }).then(() => {console.log('then2222222')});
 console.log("async1 end");
}

async  function async2() {
 return Promise.resolve('sssss').then(res=>console.log(res));
}

console.log("script start");

setTimeout(function () {
 console.log("settimeout");
},0);

async1();

new Promise(function (resolve) {
 resolve(console.log("promise1"));
}).then(function () {
 console.log("promise2");
});

console.log('script end');

执行结果

script start
async1 start
promise1
script end
sssss
promise2
promise11111
async1 end
then2222222
settimeout


说明:当async2返回promise时,会先将async2的then放入微任务队列中,async1中await async2()
下面的代码暂时不放入微任务队列中,待async2执行完,再将后面的代码放入微任务队列中,注意:此时微任务队列还有promise2要执行

await 标志着js会去先执行一遍后面紧跟的函数,然后马上让出线程,跳出整个async,去执行本轮执行周期里面的任务,等待本轮的宏任务(同步任务)执行完成之后,再回来async里等待之前await的函数的返回值,如果是返回值异步promise,那么会把它塞入promise.resolve()微任务(异步任务执行栈),等待它前面的异步任务执行完毕之后,再得到await promise.resolve()的值,然后才是去执行await后面的逻辑,如果await 函数体返回的不是异步promise,那么就直接去执行其后面的逻辑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值