【问题标题】:async.whilst - pausing between callsasync.whilst - 在通话之间暂停
【发布时间】:2017-02-08 19:48:04
【问题描述】:

我有一个需要多次调用的函数,而不是使用 for 循环,我使用的是 async.whilst。但我需要的是,在上一次调用完成之前不会对函数进行下一次调用,这不是 async.whilst 发生的情况。有没有办法实现这一点(我使用 setTimeout 在每次调用之间暂停,但它不是很干净)。 非常感谢,C

【问题讨论】:

  • 始终共享代码,这样可以更好地了解确切的问题
  • 我会尝试重写代码以便可以使用eachLimit,但这取决于您的代码。或递归。

标签: node.js asynchronous while-loop synchronization


【解决方案1】:

我会使用永久构造。假设您的函数名称是 myFunction 并接受回调作为参数:

var count = 0;
var limit = 10; // set as number of the execution of the function

async.forever(
    function(next) {
        myFunction(function () {  
          count++;
          if(count < limit) {
            next();
          } else {
            next(true);
          }
        })
    },
    function(ended) {
        // function calling iteration ended
    }
);

【讨论】:

  • 非常感谢 - 它似乎做到了。 NodeJS 是一个非常不同的范式,所以真的需要一些时间来适应它...... C
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多