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).
January 22, 2009 at 12:27 pm
Very nice explanation.
Thanks
February 2, 2009 at 10:12 am
Hello,
Nice Example…
Thanks…
April 9, 2009 at 7:57 pm
Hi budy, give some more examples.
May 26, 2009 at 2:56 pm
Hi arshad,
Thanks for the explaination.. I was looking for such a brief and clear explaination…
November 20, 2009 at 1:36 pm
Nice example. My confusion abt many subject has been removed because of this example. Thanks write. thanks
December 7, 2009 at 5:58 pm
Hi nice information.thanks
December 12, 2009 at 8:50 am
I’m always searching for new blogposts in the internet about this subject. Thanks.
December 21, 2009 at 1:18 pm
Superb
March 29, 2010 at 7:54 pm
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
March 29, 2010 at 8:06 pm
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
June 1, 2010 at 2:08 pm
Nice Explain . but expect more.
July 20, 2010 at 3:27 pm
Very nice example. Also want simple Asynchronous AJAX example
January 20, 2011 at 12:03 pm
That is very useful post, same question has been asked to me in an interview and now i know what rubbish i was answered…
February 26, 2011 at 1:44 am
very good example
keep it up
thanks
March 7, 2011 at 3:02 pm
Nice explanation buddy….
December 6, 2011 at 7:02 pm
Very understandable explanation…. Thanks!
January 3, 2012 at 12:10 pm
Very nice explanation.. short and very understandable… Thanks
January 23, 2012 at 1:14 pm
[…] post is copied from my first blog which was posted almost 4 years […]
February 7, 2012 at 10:08 pm
Simple and Powerful !
September 15, 2012 at 1:36 am
thanks…
July 12, 2013 at 2:10 am
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.
July 16, 2013 at 7:26 pm
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 .
March 22, 2014 at 2:55 am
great dude superb explanation!!!
June 13, 2016 at 8:34 pm
This is really nice tips, keep posting
August 5, 2017 at 5:18 pm
Mind blowing…Thanx a lot..