What is the difference between asynchronous (AJAX) and synchronous request?

May 25, 2008

Now-a-days, this is a very common question that been asked in all most every basic interview – What is the main strength of AJAX? Or why and when should we use AJAX in web application?
And the most common answer is – “AJAX does not refresh or reload the whole page”.

But, the more perfect answer is – “AJAX is an asynchronous technology where others request are synchronous.”

So, what is the difference?

In a word, a program does not wait for response after requesting an asynchronous call whereas synchronous does so.

Here is a simple example –

function check() {
var a=0;
a = getStatus(“getstatus.php?id=5”);
if(a==1) {
alert(“active”);
} else {
alert(“not active”);
}
}

Here getStatus() function sends a AJAX request to the server with “getstatus.php?id=5” url and the php file decides (from database may be) the status and output/response as 1 or 0.

But, this function will not work properly. It will alert “not active” instead of “active”. And yes, that is for the asynchronous request.

The reason is – when a = getStatus(“getstatus.php?id=5”); line is being executed program does not wait for the response of setStatus() function. So, value of keep unchanged or set to null.

So, how should we work with asynchronous request?

Of course, using callback function. Callback function is that function which is triggered when the request completes to get the response (or as defined).

25 Responses to “What is the difference between asynchronous (AJAX) and synchronous request?”

  1. Neelesh Says:

    Very nice explanation.

    Thanks

  2. avinash Says:

    Hello,

    Nice Example…

    Thanks…

  3. Kaushal Says:

    Hi budy, give some more examples.

  4. nishit Says:

    Hi arshad,
    Thanks for the explaination.. I was looking for such a brief and clear explaination…

  5. Rafique Says:

    Nice example. My confusion abt many subject has been removed because of this example. Thanks write. thanks

  6. pritam m Says:

    Hi nice information.thanks

  7. kexassedy Says:

    I’m always searching for new blogposts in the internet about this subject. Thanks.

  8. sasikumar Says:

    Superb

  9. PiterJankovich Says:

    My name is Piter Jankovich. oOnly want to tell, that your blog is really cool
    And want to ask you: is this blog your hobby?
    P.S. Sorry for my bad english

    • rshad Says:

      Thank you very everybody for your comments and appreciations.

      @PiterJankovich,
      Yes, Blogging is my hobby, but I don’t have enough time to write blog regularly. Hopefully, Soon – I will begin writing blog regularly.

      Thanks Again
      Arshad

  10. Manoj Says:

    Nice Explain . but expect more.

  11. Dipak Says:

    Very nice example. Also want simple Asynchronous AJAX example

  12. Atul kushwah Says:

    That is very useful post, same question has been asked to me in an interview and now i know what rubbish i was answered…

  13. upendra Says:

    very good example
    keep it up
    thanks

  14. Raghu Says:

    Nice explanation buddy….

  15. Chella Says:

    Very understandable explanation…. Thanks!

  16. rajasekar Says:

    Very nice explanation.. short and very understandable… Thanks

  17. Yuvi Says:

    Simple and Powerful !

  18. shareeef Says:

    thanks…

  19. seoknowledgebasesanpad Says:

    Excellent explanation. However, here is an explanation that explains this concept in the most basic fashion:

    Asynchronous communication works much like the postal system: An application creates a message (that’s a piece of data such as the text String “Order 1000 barrels crude oil”, or an XML expression), and labels the message with a destination address (that’s typically the logical name of a “mail box”, and not an IP address). The message is passed to the messaging middleware system.

    Now the sender application proceeds happily, without needing to wait for the message to be delivered.

    Synchronous communication works much like a phone call. The Receiver (callee) must be available, otherwise the conversation cannot occur.

  20. Kushal Says:

    really thats outstanding answer of my all question.
    Now i have no more question regarding the AJAX.
    thnk.you thnkyou thnkyyou very much .
    if you dont mind then plz send me some tuts on JQuery .

  21. Ankush Jerly Says:

    great dude superb explanation!!!

  22. Bobby Zauner Says:

    This is really nice tips, keep posting

  23. Kumar Sumit Says:

    Mind blowing…Thanx a lot..


Leave a reply to Atul kushwah Cancel reply