红灯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()
            })
        }

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2021-11-29
  • 2022-01-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-10
  • 2021-09-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2021-12-12
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
相关资源
相似解决方案