zoukankan      html  css  js  c++  java
  • 使用Promise实现红绿灯交替重复亮

    红灯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()
                })
            }
    
  • 相关阅读:
    mysql基础
    协程
    锁与队列
    线程练习
    线程
    并发编程
    进程
    操作系统发展史
    第六周----例行报告
    附加题找bug
  • 原文地址:https://www.cnblogs.com/samsara-yx/p/12626330.html
Copyright © 2011-2022 走看看