主题
模拟红绿灯
红灯3s,绿灯2s,黄灯1s,如此循环
async await 实现
js
async function task (list) {
for (let i = 0; i < list.length; i++) {
await light(...list[i])
}
await task(list)
}
async function light(s, time) {
console.log(`${s} 开始了`)
await new Promise(resolve => {
setTimeout(resolve, time)
})
console.log(`${s} 结束了`)
}
const list = [
['红灯', 3000],
['绿灯', 2000],
['黄灯', 1000]
]
task(list)