javascript - Node.js async.whilst() 根本没有执行

标签 javascript asynchronous

我只是尝试使用 async.whilst(),如 here 所示.

这是我的简单代码,取自他们的文档:

var async = require('async');

console.log('start');
async.whilst(
  function () { return true; },
  function (callback) {
    console.log('iteration');
    callback();
  },
  function (err) {
    console.log('end');
  },
);

当我运行它时,循环没有运行。只有 start 被打印出来。

最佳答案

因为您返回的是 true,所以没有调用函数 1 的回调。所以你只能看到“开始”。 您可以在下面找到一些信息:

    const async = require('async');
    let count = 0;
    const compareVariable = 10;
    console.log('start');
    async.whilst(
        function functionName1(callbackFunction) {
            // perform before each execution of iterFunctionInside, you need a condition(or other related condition) in 2nd params.
            callbackFunction(null, count < compareVariable)
        },
        // this func is called each time when functionName1 invoked
        function iterFunctionInside(callback) {
            // increase counter to compare with compareVariable
            count++;
            console.log('iteration', count);
            // if you want to show like tick time, you can set timeout here or comment out if you dont want
            setTimeout(() => {
                callback(null, count);
            }, 1000)
        },
        function (err, n) {
            console.log('end');
        },
    );

关于javascript - Node.js async.whilst() 根本没有执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246825/

相关文章:

javascript - 如何防止子节点上的父事件处理程序?

javascript - float 悬停而不是点击事件

javascript - 像 Pinterest 一样接管浏览器滚动条的模态窗口

javascript - 如何仅在 "map"循环完成时设置状态?

Java 多线程与 CompletableFuture 运行速度较慢

ios - 按顺序异步下载图片

java - 如何正确使用 wait 和 notify 方法?

javascript - 使用 ramdajs 创建前 5 个聚合

javascript - 如何使一个 div 中的三个表在调整大小时不重叠?

JavaScript。在前端使用 async 和 wait 安全吗?