简体   繁体   English

等待Java中异步函数调用的最有效方法是什么?

[英]What's the most efficient way to wait for an asynchronous function call in Javascript?

This might be the opposite of my previous question here but anyway, I need to know its answer as well. 这可能与我之前在这里提出的问题相反,但是无论如何,我也需要知道它的答案。

I have an Ajax call in my algorithm that I need to wait for it to run some code. 我的算法中有一个Ajax调用,我需要等待它运行一些代码。 There are two solutions: 有两种解决方案:

1) The typical solution: 1)典型的解决方案:

ajaxCall(function(result){
  //the code to run after the call is returned
});

2) The one I'm wondering if it can be an alternative: 2)我想知道它是否可以替代:

res=null;
ajaxCall(function(result){
  res=result;
});
while(res==null)/*wait! but this will lock the GUI I guess*/;
//do the rest of the code because now res is initialized

The question is how can I write the second solution in an efficient way that doesn't freeze the GUI? 问题是我该如何以不冻结GUI的有效方式编写第二个解决方案?

I suggest hooking all dependent code to execute as a callback from your ajax call 's return. 我建议挂接所有相关代码以从ajax调用return中作为回调执行。 That way, all other javascript can continue to execute and you will not make your browser unresponsive for the duration of the call. 这样,所有其他javascript都可以继续执行,并且您不会在通话期间使浏览器无响应。

Alternatively, which is not something I would never ever do, you can make your ajax call synchronous, using async: false like so: 另外,这不是我永远不会做的事情,您可以使用async: false来使ajax调用同步,如下所示:

$.ajax({ url: ..., async: false });

Just make the ajax call synchronous. 只需使ajax调用同步即可。

ref: http://developer.mozilla.org/en/XMLHttpRequest 参考: http : //developer.mozilla.org/en/XMLHttpRequest

look for the async parameter 寻找异步参数

A generic answer: 通用答案:

There are only two methods available in async. 异步中只有两种方法可用。 programming: events and callbacks. 编程:事件和回调。 Period. 期。

(Technically speaking, there is not really a difference between the two on the lowest level, an "event" is just a (library) function - the one doing the event "firing" - executing all functions registered as listeners so it's the same as a callback - technically, ie when you write event.fire() or whatever the event syntax of your preferred lib it's synchronous invocation of all registered event handler functions). (从技术上讲,两者在最低级别上并没有真正的区别,“事件”只是一个(库)函数-执行事件“触发”的那个函数-执行所有注册为侦听器的功能,因此与回调-从技术上讲,即,当您编写event.fire()或首选库的任何事件语法时,它都是对所有已注册事件处理程序函数的同步调用)。 When to use one or the other is a matter of preference, convention and style when designing an API.) 在设计API时,何时使用哪种取决于偏好,约定和样式。)

Javascript programming, especially AJAX, is asynchronous by definition. Java编程,尤其是AJAX,在定义上是异步的。 So if you have an algorithm that needs to "wait" for something, you're better off reconsidering the algorithm. 因此,如果您有一种算法需要“等待”某些东西,那么最好重新考虑该算法。 Ironically enough, Javascript syntax is not best suitable for async programming, but there are many libraries that help you keep callbacks under control and avoid spaghetti code. 具有讽刺意味的是,Javascript语法并非最适合异步编程,但是有许多库可帮助您控制回调并避免使用意大利面条式代码。

Example of callbacks spaghetti: 意大利面条回调的示例:

function ajaxRequest(url1, function() {
    animateSomething(div, function() {
        ajaxRequest(url2, function() {
            ....
        })
    })
})

the same with async.js looks much cleaner: async.js相同看起来更干净:

async.series([
    function(_) { ajaxRequest(url1, _) },
    function(_) { animateSomething(div, _) },
    function(_) { ajaxRequest(url2, _) }
])

there are many ways to do this one. 有很多方法可以做到这一点。 one of the is passing a callback to the ajax (or at least a reference of it). 其中之一是将回调传递给ajax(或至少是其引用)。 your code #1 would be an example of that. 您的代码#1就是一个例子。

another is that you have a notifier object which you add the ajax success call to it. 另一个是您有一个通告程序对象,向其添加了ajax成功调用。 then you can have other functions (one or more) plug into it to listen for a "success" announcement. 那么您可以将其他功能(一个或多个)插入其中,以收听“成功”通知。

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

相关问题 使用 JavaScript 调用 Node.js 后端函数的最有效方法是什么 - What's the most efficient way to call a Node.js backend function with JavaScript 在JavaScript中调用多重延迟的最有效方法是什么? - What is the most efficient way to call multiple delays in Javascript? 使用JavaScript评估字符串是否是回文符的最有效方法是什么? - What's the most efficient way to evaluate if a string is a palindrome using Javascript? 在Java中通过字符串的最有效方式是什么? - What is the most efficient way to go through string's chars in Javascript? 在JavaScript中,将标签转换为链接的最有效方法是什么? - In javascript, what's the most efficient way to I turn tags into links? 比较 javascript 中的 2 个元素的最有效方法是什么? - What's the most efficient way to compare 2 elements in javascript? 在Javascript中创建嵌套div的最有效方法是什么? - What is the most efficient way to create nested div's in Javascript? 在 JavaScript 中处理显示对话框/模式的最有效方法是什么? - What's the most efficient way to handle displaying a dialog/modal in JavaScript? 在Javascript中声明函数的最有效方法是什么? - What is the most efficient way to declare functions in Javascript? 在Javascript中实现GroupBy的最有效方法是什么? - What is the most efficient way to implement GroupBy in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM