Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX ASYNC False vs. True

Tags:

jquery

ajax

I have a test site here (kdmalikdesign.com/test/rsd/index.html). I am in the middle of doing a bunch of things with it. My main concern is right now it does not work unless ASYNC is FALSE which I hear is bad practice?

Now I have determined the reason that async is false is because when i fire the success callback the xml data isn't loaded but when I load a completed callback everything loads fine. I had to change it to async false to get it to work correctly with the success callback.

Is there a specific way to go about doing this? Basically what the ajax call is doing is grabbing an xml file and reading through it depending on the filename populating the page with specific data. I am doing it basically as practice/excercise.

Thank you, Kamron

like image 617
kmalik Avatar asked May 15 '13 15:05

kmalik


People also ask

What is difference between async true and false in AJAX?

by default async is true. it means process will be continuing in jQuery ajax without wait of request. Async false means it will not go to next step untill the response will come. By default async is true process will be continuing in jQuery ajax without wait of request.

What does async false do in AJAX call?

Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it's execution and the next statement will be called regardless of whether the async statement has completed yet.

Is Ajax async false deprecated?

As of jQuery 1.8, the use of async:false in jQuery. ajax() is deprecated.

Why Synchronous XMLHttpRequest async false is not recommended?

Synchronous XMLHttpRequest (async = false) is not recommended because the JavaScript will stop executing until the server response is ready. If the server is busy or slow, the application will hang or stop.


1 Answers

Running a synchronous call is usually a malpractice, as you are effectively running a request while losing all the benefits for asynchronicity, when you could be using callbacks to do the same thing in an asynchronous fashion.

async:false will cause the jQuery.ajax() call to block until it returns. Effectively, in pseudocode, instead of this:

function ajax:
   perform request
   callback with results

You are doing this:

function ajax:
   perform request
   while (no results) wait
   return results

This completely blocks the execution of anything else until this is over...which is pretty horrible. The obvious use case for it is running stuff in a waterfall pattern: task 1 -> task 2 -> task 3, which can happen.

If you can afford to block your browser, still consider using callbacks. They will allow you to keep the rest of your site active and well, while processing stuff. You can easily do this by setting async:true and providing a callback with your next step. You may end up in a callback spaghetti, however, and may want to use a library to manage large operations if you have them.

A very good candidate for this hails from Node.JS and is called async.js. It is the tool for MapReduce stuff, and implements both waterfall and parallel running models.

Morale of the story: async:false can 100% of the time be replaced with a callback.

like image 73
Sébastien Renauld Avatar answered Sep 20 '22 23:09

Sébastien Renauld