Promise 与async
var p1=new Promise((resolve,reject)=>{
setTimeout(()=>{console.log("first");resolve()},1000)
})
var p2=new Promise((resolve,reject)=>{
setTimeout(()=>{console.log("two");resolve()},2000)
})
var p3=Promise.all([p1,p2])
p3.then((res)=>{
console.log('over')
setTimeout(()=>{ console.log(res+" two")},1000)
})
请求p1,p2,都结束后执行请求p3
async function add(a){
let p1=await new Promise((resolve,reject)=>{setTimeout(()=>{console.log(a);resolve(a)},1000)})
return p1
}
add('kal').then((res)=>{ setTimeout(()=>{console.log(res+'rese')},1000) })
请求结束后执行then ()
网友评论