老规矩,先放两道题目,先看看会有什么结果输出,然后我们一起研究为什么
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*/
未完,待续...