码迷,mamicode.com
首页 > 其他好文 > 详细

宏任务与微任务

时间:2019-03-08 09:32:13      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:await   为什么   star   script   The   OLE   rip   out   promise   

老规矩,先放两道题目,先看看会有什么结果输出,然后我们一起研究为什么

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");
/*script start
async1 start
async2
 promise1
script end
async1 end
 promise2
undefined
setTimeout/*
new Promise(function(resolve){
   console.log('2');
}).then(function(){
   console.log('3')
});

console.log('4');

//2
//4
new Promise(function(resolve){
    console.log('2');
    resolve();
}).then(function(){
    console.log('3')
});

console.log('4');

//2
//4
//3
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();
script start
async1 start
async2
async1 end
Promise?{<resolved>: undefined}
setTimeout*/

未完,待续...

宏任务与微任务

标签:await   为什么   star   script   The   OLE   rip   out   promise   

原文地址:https://www.cnblogs.com/smart-girl/p/10493850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!