function p1(){ return new Promise(function(resolve,reject){ setTimeout(function(){ console.log("p1_3000"); resolve() },3000) }) }function p2(){ return new Promise(function(resolve,reject){ setTimeout(function(){ console.log("p2_2000"); resolve() },2000) })}
//await 只能出现在异步函数里 async function f(){ console.log("0000") await p1(); await p2(); console.log("1010") } p3=f(); p3.then(function(){ console.log("结束") })
console.log(1);console.log(2);setTimeout(function(){ console.log(3)},3000);console.log(4);console.log(5)
上边的打印结果大家想必都知道,这是同步状态下的执行顺序
//在看一个栗子function p1(){ return new Promise(function(resolve,reject){ setTimeout(function(){ console.log("p1_3000"); resolve() },3000) })}function p2(){ return new Promise(function(resolve,reject){ setTimeout(function(){ console.log("p2_2000"); resolve() },2000) })}
console.log("0000"); p1(); p2(); console.log("1010") //跟大家预期的结果一样,执行按照如下顺序输出 //0000 //1010 //p2_2000 //p1_3000
咱们想做的是让他按这个顺序执行下来咱们就要用到