紅燈3秒亮一次,黃燈2秒亮一次,綠燈1秒亮一次;如何讓三個燈不斷交替重復亮燈?(用Promise實現)
function red() {
console.log('red');
}
function green() {
console.log('green');
}
function yellow() {
console.log('yellow');
}
function light(cb, timer) {
return new Promise(resolve => {
setTimeout(() => {
cb();
resolve()
}, timer);
})
}
function step() {
Promise.resolve().then(() => {
return light(red, 3000)
}).then(() => {
return light(green, 2000)
}).then(() => {
return light(yellow, 1000)
}).finally(() => {
return step()
})
}