1-1 promise和定时器执行顺序
promise 会优先于 定时器
console.log("3");
setTimeout(()=>{
console.log("2");
})
Promise.resolve().then(()=>{
console.log("1");
})
/*
3
1
2
*/
1-2 await
await 阻塞线程 会从异步函数中跳出来,优先执行同步的代码
async function show(){
console.log(1);
var res = await 100;
console.log(res);
}
show()
console.log(2); // 1 2 100
setTimeout(()=>{
console.log(3);
},1000)
/*
1
2
100
3
*/
async function show(){
console.log("script-start");
var res = await go()
console.log(res);
}
async function go(){
return 1
}
show()
setTimeout(()=>{
console.log(4);
},1000)
var p = new Promise((resolve,reject)=>{
console.log(2);
resolve(3)
})
p.then((res)=>{
console.log(res);
})
console.log("script-start");
/*
script-start
2
script-start
1
3
4
*/