8

Why is AJAX called asynchronous? How does it accomplish communication asynchronously with the server?

Tiny
  • 27,221
  • 105
  • 339
  • 599
Bhavesh
  • 303
  • 2
  • 3
  • 9

6 Answers6

24

It's asynchronous in that it doesn't lock up the browser. If you fire an Ajax request, the user can still work while the request is waiting for a response. When the server returns the response, a callback runs to handle it.

You can make the XMLHttpRequest synchronous if you want, and if you do, the browser locks up while the request is outstanding (so most of the time this is inappropriate)

Community
  • 1
  • 1
hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • 3
    -1 Asynchronous means "not at the same time". You didn't explain what was "not at the same time" about AJAX. Your answer makes it sound like you can work at the same time as the call, so therefore it is called asynchronous, which makes no sense grammatically. – gcdev Feb 13 '15 at 18:24
  • @gcdev I think this confusion arises because, as previously mentioned [here](https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean#comment57854159_748189), "Asynchronous" and "Synchronous" in this context do not refer to the timing relationship between two or more tasks; rather they refer to the relationship between a task and the clock. Therefore, this answer makes sense. – today Jan 11 '18 at 10:21
  • Yes it is confusing since the dictionary defines it as *existing or occurring at the same time period*. Asynchronous would therefore mean *not* happening in the same period -- but this **is clearly not so** with user tasks employing an XMLHttpRequest object. Unlike what @today said, you do need to look at how user requests are accepted *with respect to previously submitted requests*. From this standpoint, synchronous request acceptance demands that user request B must await completion of some prior request A. Asynchronous allows sending requests with no regard to the status of prior requests. – Trunk Mar 04 '19 at 12:49
19

It's asynchronous because the client and the server run independently of each other for the duration of the function call.

During a normal function call, you make the call, and the calling function doesn't get to execute again until the function call finishes and returns. The caller and the callee are always synchronized.

During an asynchronous function call, you make the call, and then control returns immediately to the caller. The callee then returns a value some indeterminate amount of time later. That "indeterminate amount of time" means the caller and callee are no longer synchronized, so it's asynchronous.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thank you -- I finally understand what exactly is considered "synchronous" and "asynchronous"! It has always puzzled me that when you run two AJAX calls at the same time (synchronized) they are called "asynchronous". You explained it nicely. – gcdev Feb 13 '15 at 18:25
0

I.e. not "blocking", within the context of Javascript execution, as the response will be handled by an event loop.

eclewlow
  • 931
  • 9
  • 9
0

Simply put, it does not need to reload the whole page to get new information. Think of a email client. You would not need to refresh the page to see new emails. Ajax just pulls the server every couple of minutes to see if there are new emails, if so display them

WindowsMaker
  • 3,132
  • 7
  • 29
  • 46
  • 1
    IMHO this answer gives a wrong picture of what AJAX, and asynchrony in general, is. Neither AJAX nor asynchrony are equal to simple repeated polling to prevent the user from having to refresh a page manually. Asynchrony is about not blocking (e.g. not blocking further page/script processing while some resource request is pending). This answer is correct in the sense that apart from just asynchrony, AJAX also makes it possible to load new data and update the current page without loading a completely new page. – stakx - no longer contributing Dec 05 '14 at 20:49
-1

The client and the server run independently of each other for the duration of the function call.

Normal function call - you make the call, and the calling function doesn't get to execute again until the function call finishes and returns. The caller and the callee are always synchronized.

Asynchronous function call - you make the call, and then control returns immediately to the caller. The callee then returns a value some undefined amount of time later. That "undefined amount of time" means the caller and callee are no longer synchronized, so it's asynchronous.

-1

Synchronous always maintain sequence when called, but asynchronous is not maintain sequence.