红绿灯任务控制
function red(){ console.log('red')}function yellow(){ console.log('yellow')}function green(){ console.log('green')}const task = (timer, light) => { return new Promise((resolve, reject) => { setTimeout(() => { if(light === 'red'){ red() }else if (light === 'yellow'){ yellow() }else if (light === 'green'){ green() } resolve() }, timer) })}// promiseconst step = () => { task(3000, 'red').then(() => { task(1000, 'yellow') }).then(() => { task(2000, 'green') }).then(step)}step()// async、awaitconst step = async () => { await task(3000, 'red') await task(1000, 'yellow') await task(2000, 'green') step()}step()