javascript - while循环javascript的回调方法

标签 javascript node.js loops asynchronous callback

我想在 while 循环中使用回调函数,如下所示:

do {
    Methodwithcallback(..function () {
        //do something  
    });
}
while ();

function() 不会被调用,但它在没有 loop 的情况下工作。

方法需要一些时间来执行,我想在方法完成后重复循环

为什么方法在循环中被忽略了?我正在使用 Node.js

最佳答案

您不能像这样使用带有异步函数的循环 - 循环要“现在”执行,而回调要“稍后”执行。

尝试这样的事情:

function callback() {
    // do whatever else is needed
    methodWithCallback(callback); // start the next one
}

methodWithCallback(callback); // start the first one

关于javascript - while循环javascript的回调方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47517061/

相关文章:

java - 如何将 JavaScript 数组转换为 Java 数组

php - 使用 grunt 更新 php 文件中的版本号

java - 编译保持运行(模式、匹配器任务)

python - 使用 groupby 创建新数据框

Php mySQL 在 2 个表的循环内循环

javascript - 将参数内存为键

javascript - 如何在一次调用中分配多个 html 元素值?

javascript - 使用 RequireJS 加载 WebEssentials 生成的缩小文件

javascript - 将 Angular 应用程序作为 Express Server 中的静态内容提供服务

node.js - 将 AngularJS 应用程序部署到普通 Apache HTTP 服务器是一种常见的选择吗?