我想在 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/