简体   繁体   English

while循环内的JavaScript回调不起作用?

[英]Javascript callback within a while loop does not work?

I'm trying to make an event driven while loop in javascript. 我正在尝试在javascript中进行事件驱动的while循环。 Essentially it iterates through an array of values and makes a query during each iteration. 本质上,它会迭代一个值数组,并在每次迭代期间进行查询。 Then, based on the result of the query it should either keep looping or stop and do something. 然后,根据查询结果,它应该继续循环或停止并执行某些操作。

I realize that you can't have callbacks in a while loop so I'm confused on how to even implement such a thing. 我意识到你不能在while循环中有回调,所以我对如何实现这样的事情感到困惑。 Nested functions repeat themselves and that's not ideal. 嵌套函数会重复出现,这并不理想。

my loop code: 我的循环代码:

             var done = false;
             while(!done){
                bridgeCheckRooms(send,function(reply){ //callback
                    console.log(tagArray[count]);
                    console.log(reply);
                    if(reply.success){
                        done = true;
                    } else {
                        count--;
                    }
                });
            }

You should use recursion. 您应该使用递归。 An approach to that would be something like this: 一种方法是这样的:

     function next() {

            // ....

            bridgeCheckRooms(send,function(reply){ //callback
                console.log(tagArray[count]);
                console.log(reply);
                if(reply.success){
                    // continue() ?
                    // done = true;
                } else {
                    count--;
                    next();
                }
            });
     }

     next();

Of course this is just an example. 当然,这只是一个例子。 You should adapt it to you entire code. 您应该使它适应您的整个代码。

Your code would be correct if bridgeCheckRooms was synchronous and blocked the thread of execution until the response was returned. 如果bridgeCheckRooms是同步的并且阻塞了执行线程,直到返回响应,您的代码将是正确的。 See this SO post on synchronicity 看到这篇关于同步性的帖子

Here is what is happening in your code: 这是您的代码中正在发生的事情:

The code block in your while loop is executing bridgeCheckRooms which we will assume does some work, then queues an asynchronous call to the database. while循环中的代码块正在执行bridgeCheckRooms ,我们假设它将做一些工作,然后将对数据库的异步调用排队。 bridgeCheckRooms will then return, and execution of your while loop will immediately continue, and queue up yet another call to bridgeCheckRooms . 然后, bridgeCheckRooms将返回,并且while循环的执行将立即继续,并将另一个对bridgeCheckRooms调用bridgeCheckRooms

They key piece is that bridgeCheckRooms is an asynchronous function, and doesn't block execution until it is finished. 他们的关键是bridgeCheckRooms是一个异步函数,并且直到完成后才阻止执行。 So when you call bridgeCheckRooms, it simply queues an asynchronous call, but doesn't wait until it completes. 因此,当您调用bridgeCheckRooms时,它只是将一个异步调用排队,但是不会等到完成。

So if your while loop executes 10 times, you will have 10 pending requests to the database. 因此,如果while循环执行10次,您将有10个对数据库的待处理请求。 But those requests won't get to execute until the while loop stops executing and yields execution for the asynchronous queue to execute. 但是,直到while循环停止执行并让异步队列执行后,这些请求才开始执行。

@pablo has the right code approach for this. @pablo为此具有正确的代码方法。 The difference between his code and your code, is that a second asynchronous call is not made until the first asynchronous call has completed. 他的代码和您的代码之间的区别在于,在第一个异步调用完成之前,不会进行第二个异步调用。

Edit: CallbackHell.com looks like a great primer on this topic 编辑: CallbackHell.com看起来像是关于这个主题的很棒的入门

I think you want to look into deferred objects and promises. 我认为您想研究延迟的对象和承诺。 You want to wait until the async call is done to make your check to see if you need to keep looping. 您要等到异步调用完成后再进行检查,以查看是否需要继续循环。

Or you could make it a synchronous call if there is not benefit to the async here. 或者,如果此处对异步没有好处,则可以使其成为同步调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM