function red(){
console.log('red')
}
function green(){
console.log('green')
}
function blue()
{
console.log('blue')
}//1.回调函数方法
function showTask(light,delay,callback){
setTimeout(()=>{
if(light==='red'){
red()
}else if(light==='green'){
green()
}else{
blue()
}
callback()
},delay)
}
function step(){
showTask('red',3000,()=>{
showTask('green',2000,()=>{
showTask('blue',1000,step)//又调用step函数实现循环调用(打印)
})
})
}
//step()
//2.使用promise实现
function showTask2(light,delay){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
if(light==='red'){
red()
}else if(light==='green'){
green()
}else if(light==='blue'){
blue()
}
resolve(light)
},delay)
})
}
function step2(){
showTask2('red',3000)
.then((value)=>showTask2('green',2000))
.then((value)=>showTask2('blue',1000))
.then(step2)
}
//step2()
//3.async/await
async function step3(){
await showTask2('red',3000)
await showTask2('green',2000)
await showTask2('blue',1000)
step3()
}
//step3()
console