代码之家  ›  专栏  ›  技术社区  ›  Nick

在NodeJS中使用Promise递归创建无限循环

  •  0
  • Nick  · 技术社区  · 8 年前

    显然,在while循环中承诺不起作用,所以我想知道如何使用递归返回到 run() 函数并增加我的计数器 x ?

    var x = 0
    var sign = 1
    function run() {
        return new Promise((resolve, reject) => {
            try {
                wait(20).then(() => { device.setColor("random", { 'channel': 0, 'index': x }) })
                wait(20).then(() => { device.setColor("#000000", { 'channel': 0, 'index': x }) })
                x += sign
                if (x == LED_COUNT - 1) {
                    sign = -1
                } else if (x == 0) {
                    sign = 1
                }
                return resolve()
            } catch (e) {
                return reject(e)
            }
        })
    }
    run() // this only runs once, I need it to run forever
    
    3 回复  |  直到 8 年前
        1
  •  2
  •   Dalin Huang    8 年前

    递归调用:

    添加 .then(run())

    我不确定等待是如何工作的,我使用setTimeout将每个循环延迟1秒。以下示例:

    var x = 0
    var sign = 1
    function run() {
        return new Promise((resolve, reject) => {
            try {
                //example only
                console.log('x-->' + x);
                //wait(20).then(() => { device.setColor("random", { 'channel': 0, 'index': x }) })
                //wait(20).then(() => { device.setColor("#000000", { 'channel': 0, 'index': x }) })
                x += sign
                if (x == LED_COUNT - 1) {
                    sign = -1
                } else if (x == 0) {
                    sign = 1
                }
                return resolve()
            } catch (e) {
                return reject(e)
            }
        }).then(setTimeout(function(){ run() }, 1000));
    }
    run();
        2
  •  1
  •   Amit Wagner    5 年前

    不知道你为什么需要承诺。

    var x = 0
    var sign = 1
    
    function run() {
    
        setTimeout(function () {
            device.setColor("random", {'channel': 0, 'index': x})
            device.setColor("#000000", {'channel': 0, 'index': x})
            if (x == LED_COUNT - 1) {
                sign = -1
            } else if (x == 0) {
                sign = 1
            }
    
            run()
        }, 20000)
    
    
    }
    run()
        3
  •  0
  •   MinusFour    8 年前

    我想你会希望等待这两个承诺,然后再次运行,对吗?这里有一种方法你可以试试。如果您有权访问 Promise.try 用它代替 Promise.resolve().then(...

    var x = 0
    var sign = 1
    
    function run() {
      return Promise.resolve().then(function() {
        x += sign
        if (x == LED_COUNT - 1) {
          sign = -1
        } else if (x == 0) {
          sign = 1
        }
        return Promise.all([
          wait(20).then(() => {
            device.setColor("random", {
              'channel': 0,
              'index': x
            })
          }),
          wait(20).then(() => {
            device.setColor("#000000", {
              'channel': 0,
              'index': x
            })
          })
        ]);
      }).then(run);
    }
    run()

    您也可以将这两个函数组合在一个函数中,并且只使用一个函数 wait(20) 承诺:

    return wait(20).then(() => {
        device.setColor("random", {
            'channel': 0,
            'index': x
        });
        device.setColor("#000000", {
            'channel': 0,
            'index': x
        });
    })
    
    推荐文章