AJAX 请求同步与异步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1052453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:48:15  来源:igfitidea点击:

AJAX requests Synchronous Vs Asynchronous

ajaxasynchronousrequestsynchronous

提问by rahul

Is there any difference in performance ( speed wise ) between a synchronous request and and asynchronous request?

同步请求和异步请求之间的性能(速度方面)有什么区别吗?

What all are the reasons for using an asynchronous request??

使用异步请求的所有原因是什么??

回答by Thomas Hansen

You should mostly always use an asynchronous Ajax Request, in fact I know only about one place where a synchronous Ajax Request should be used which is if you're in an Ajax Request embedding a new JavaScript file on the client and then referencing types and/or objects from that JavaScript file in the return from the original Ajax Request. Then the fetching of this new JS file should (or can sanely) be included through using a synchronous Ajax Request...

您通常应该始终使用异步 Ajax 请求,实际上我只知道应该使用同步 Ajax 请求的一个地方,即如果您在 Ajax 请求中,在客户端上嵌入一个新的 JavaScript 文件,然后引用类型和/或来自原始 Ajax 请求返回的 JavaScript 文件中的对象。然后应该(或可以理智地)通过使用同步 Ajax 请求来包含对这个新 JS 文件的获取......

Other then that you should alwaysuse asynchronous Ajax Requests. The most important reason is that a synchronous Ajax Request makes the UI (browser) unresponsive...

除此之外,您应该始终使用异步 Ajax 请求。最重要的原因是同步 Ajax 请求使 UI(浏览器)无响应...

回答by Ajay2707

@Thomas Hansen answer is right but I found a clear explanation with benefits of Asynchronous.

@Thomas Hansen 的回答是正确的,但我找到了一个清晰的解释,其中包含Asynchronous.

Synchronousis simple, but wait for the server response, thus block the execution of the caller for a period and slower than asynchronous while processing the request.

同步很简单,但是要等待服务器响应,因此在处理请求时会阻塞调用者的执行一段时间并且比异步慢。

Asynchronousis required setting, do not wait after submitting the request and immediately return control to the caller, thus faster than Synchronous.

异步是必须设置的,提交请求后不等待,立即将控制权返回给调用者,因此比同步更快。

I am coming here when I have to use upload control which has both functionalities in the question and looking the pros and cons of this functionality.

当我必须使用上传控件时,我来到这里,该控件具有问题中的两个功能并查看此功能的优缺点。

I got the another linkwhich explained with a real example. (the link is the specific tool, so understand the logic)

我得到了另一个用真实例子解释的链接。(链接是具体工具,所以理解逻辑)

  • The major difference is the response time from our servers. At the time of upload, synchronous will validate file and create the passcode in real-time. Asynchronous will send the file to our server's queue and deliver notification via email once it is processed.

  • Synchronous is not ideal for multiple, large CSV file since you will need to wait until the file is processed by the server to submit another request. On large files, this also may cause your browsers to return with timeout errors due to the server being too busy. If you have multiple files, asynchronous will allow you to submit multiple files to the server queue to be processed with a email receipt once completed.

  • 主要区别在于我们服务器的响应时间。上传时,同步将实时验证文件并创建密码。异步将文件发送到我们服务器的队列,并在处理后通过电子邮件发送通知。

  • 同步对于多个大型 CSV 文件并不理想,因为您需要等到服务器处理该文件才能提交另一个请求。在大文件上,这也可能导致您的浏览器由于服务器太忙而返回超时错误。如果您有多个文件,异步将允许您将多个文件提交到服务器队列,以便在完成后使用电子邮件收据进行处理。

https://www.aspsnippets.com/Articles/Difference-between-Synchronous-Sync-and-Asynchronous-Async-Request-Call-in-AJAX.aspx

https://www.aspsnippets.com/Articles/Difference-between-Synchronous-Sync-and-Asynchronous-Async-Request-Call-in-AJAX.aspx

回答by Prasad

there can be serious performance implications caused when it come to highly database intensive applications .. although it's very unlikely to happen .. and sending many Synchronous ajax calls can create a backlog .. so if the application database intensive and sending so many request at once it is better to make it asynchronous. when its set to asynch the browser will fail all unresponsive request and continue with new once..

当涉及到高度数据库密集型应用程序时,可能会导致严重的性能影响......虽然它不太可能发生......并且发送许多同步ajax调用可能会造成积压......所以如果应用程序数据库密集并且一次发送如此多的请求最好让它异步。当它设置为异步时,浏览器将失败所有无响应的请求并继续新的一次..